LeetCode weekly contest 126
Today I tried recording video while solving problems. Due to venue limitations, I could not use a microphone to explain, so the result was barely satisfactory. Although I could compensate a bit with text annotations, the biggest advantage of video communication was lost. In the future, I should still focus on blogging to spread my thoughts.
Especially since this time I only solved two problems. I attempted both of the last two problems, but failed on both. The video effect was too poor. If people watch videos on Bilibili, they are there to see the uploader show off. This time I did not manage to show off and instead met my Waterloo, which was quite embarrassing. But in the end I still plan to upload the video. I am just that thick-skinned: not afraid of embarrassment, and not afraid that future people will dig up my old shame.
My rank also directly flew beyond 1500. My ranking this week is probably going to drop.
Result:
| Rank | Name | Score | Finish Time | Q1 (3) | Q2 (5) | Q3 (6) | Q4 (9) |
|---|---|---|---|---|---|---|---|
| 1684 / 4564 | YoungForest | 8 | 0:38:19 | 0:16:17 | 0:33:19 WA(1) |
1002. Find Common Characters
Because I wrote the idea at the beginning while recording the video, I will only analyze the complexity here.
Time complexity: O(N), where N is the total length of all strings in A.
Space complexity: O(1), because characters are limited to 26 lowercase letters. Otherwise, it would be proportional to the number of character types.
1 | class Solution { |
1003. Check If Word Is Valid After Substitutions
Time complexity: O(N ^ 2), because find and constructing nextS are O(N). Each while loop reduces the length of S by 3, so it needs length / 3 iterations.
Space complexity: O(N), because a new nextS is constructed. This can be eliminated by repeatedly reusing the original space of S, but that would require extra code.
1 | class Solution { |
1004. Max Consecutive Ones III
After reading the discussion section, I was startled to realize that this should use sliding window. Actually, if one is familiar with this type of problem, it is still a very intuitive idea. This problem also pointed straight at a blind spot in my knowledge. To be honest, I am not familiar enough with sliding window, or two pointers, problems. When I saw this problem, I kept trying to use DP and greedy. I spent a lot of time and still failed to solve it, which is not surprising.
1 | class Solution { |
Time complexity: O(n). This is also the characteristic of sliding window: the left and right pointers each only need to move once.
Space complexity: O(1).
Also using sliding window, the code from discussion master lee215 is like this:
1 | int longestOnes(vector<int>& A, int K) { |
Because we want to find the largest window, shrinking the window is unnecessary. That is why the master’s code is so concise.
1000. Minimum Cost to Merge Stones
LeetCode’s 1000th problem is indeed very hard. I read other people’s Discuss posts for about an hour before understanding it.
To be honest, DP is also a blind spot in my knowledge. When I am lucky I can solve it; when I am not, I cannot. As Huahua said, DP is the type where even after solving 100 problems, you may still fail to solve a new one.
Because the Discuss posts were too hard to understand, after I understood it myself, I wanted to write the blog post in as much detail as possible.
There are two key points in a DP problem:
- Optimal substructure
- Overlapping subproblems
Let dp[i][j] represent the minimum cost required to merge stone[i] ~ stone[j].
Here, “merge” means merging these piles of stones until fewer than K piles remain. At that point, even if you want to continue merging, you cannot. What can be determined is that once i and j are fixed, the number of piles remaining after merging is fixed, namely (j - i + 1) % (k - 1).
At this point, the optimal substructure is $dp[i][j] = min(dp[i][mid] + dp[mid+1][j]) + (\sum_k^{i<=k<=j} stone[i] if (j - i) % (k - 1) == 0), for mid in range(i, j, k-1)$.(j - i) % (k - 1) == 0 means the piles in [i, j] can be merged into 1, so a merge operation will definitely be performed, and the cost is the total number of stones. Why is the step for mid k - 1? Because only this way does dp[i][mid] leave 1 pile remaining.
After finding the optimal substructure, the next step is to determine how to compute dp[0][n-1] bottom-up.
Initialization: dp[i][i] = 0. One pile of stones never needs to be merged.
According to the optimal substructure, we can obtain the computation order shown below:

In the figure, the red line indicates the outermost loop, the green line indicates the inner loop, and the yellow line indicates the optimal substructure.
1 | class Solution { |
Time complexity: O(N^3 / K),
Space complexity: O(N^2).
Similar problems include: 312. Burst Balloons