LeetCode weekly contest 137
This week’s problems were harder than usual. You could also say they happened to hit my blind spot: DP problems. To be honest, I have not studied DP problems deeply. This contest had especially many DP problems, especially the fourth problem, which can be solved with the classic knapsack problem.
| Rank | Name | Score | Finish Time | Q1 (4) | Q2 (5) | Q3 (6) | Q4 (8) |
|---|---|---|---|---|---|---|---|
| 576 / 4091 | YoungForest | 13 | 0:45:56 | 0:09:24 | 0:14:20 | 0:35:56 2 | null |
1046. Last Stone Weight
Intuition:
The solution to this problem is not hard. I first thought of the most brute-force solution, simulating the whole smash process. Because it is a warm-up problem, brute force is enough.
Time complexity: O(n^2 log n)
Space complexity: O(1)
1 | class Solution { |
But in fact, this is also simulating the smash process and finding the two largest stones. My method uses sort, whose time complexity is O(n log n). Stronger programmers can think of a Priority Queue, where the time complexity for finding stones is O(log n). Also, my solution does not specially handle the x == y case. Although it does not cause problems, it indeed does not match the original intent of the simulation.
Priority queue version
Time complexity: O(N log N)
Space complexity: O(1)
1 | class Solution { |
1047. Remove All Adjacent Duplicates In String
Intuition:
Use a stack to store previous characters, read the next character in order, and remove based on the condition.
Time complexity: O(N),
Space complexity: O(N).
1 | class Solution { |
Using a stack should be the standard solution for this problem. Stronger programmers basically use the same method.
1048. Longest String Chain
Intuition:
DP. For each word, iterate over all words that are shorter by 1 and update to the maximum dp + 1.
Time complexity: O(N^2 * word.length)
Space complexity: O(N).
Here N is words.length.
1 | class Solution { |
Discuss summary: there are roughly only two methods, DP (bottom-up) and DFS. DFS has higher complexity and needs memoization, which is actually top-down DP.
1049. Last Stone Weight II
Intuition:
backtracking.
Time complexity: O(N!), N = 30. So it will definitely time out.
In addition, I also tried a Greedy method.
But I did not find the correct solution or prove it. I tried twice and both were Wrong Answer.
Correct idea:
Consider a remaining stone after one smash, y - x. If it is smashed again, the remaining stone is z - (y - x) = z - y + x.
For any original stone, if the number of times it is smashed is odd, it has a negative sign in front; otherwise, it has a positive sign.
Therefore, the original problem can be transformed into splitting all stones into two groups and minimizing the absolute difference between the two groups’ sums.
This problem is one of the classic knapsack problems, “minimum knapsack partition.”
Solution: DP for classic knapsack problem
1 | class Solution { |
Time complexity: O(sum_bound * stones.size())
Space complexity: O(sum_bound)
In C++, bitset is an array containing bits or Boolean values with a fixed size. If you are not familiar with it, you can read The C++ Standard Library, 2nd Edition. I have also been reading it recently, with the goal of becoming familiar with the C++ standard library and learning its design and usage.
Besides using bitset, you can also directly use set.
Afterword
On Friday I bought a one-year LeetCode Premium subscription for 159 dollars, roughly 1000 RMB.
Time to grind problems!!!