LeetCode weekly contest 133
| Rank | Name | Score | Finish Time | Q1 (4) | Q2 (5) | Q3 (5) | Q4 (5) |
|---|---|---|---|---|---|---|---|
| 864 / 4860 | YoungForest | 14 | 1:10:35 | 0:42:32 | 0:54:38 | 1:10:35 | null |
This week’s problems improved quite a lot in quality compared with the previous few weeks. There were fewer trivial problems, and more algorithmic knowledge was tested. Even the first two Easy problems tested enough programming ability. In this contest, I lost half an hour at the beginning because of a stomachache, so I started late. The last problem was actually close to AC. The overall direction was right: using a Trie. But after submitting in the last 10 minutes, it TLE’d, and there was no time left to modify it. The construction direction of the Trie was reversed. For matching problems, we can go from front to back, or from back to front. For this problem, going from back to front is not only simpler to implement, but also better in time (even if the worst-case time complexity is the same).
1030. Matrix Cells in Distance Order
Intuition:
Sort all cells by distance.
Here I used a treemap, which keeps entries ordered after insertion. You can also construct a vector and then sort it.
Time complexity: O(R * C * log (R*C)).
Space complexity: O(R * C).
1 | class Solution { |
At first, I tried to construct the result directly, but found it was not easy to write. I decisively gave up, though it still cost some time.
After reading discuss, I found that the mainstream solution still starts from construction. But using standard BFS makes it much easier to write.
1029. Two City Scheduling
Intuition:
Greedy.
Because each person must be assigned to one city, and the result asks for the minimum total cost,
sort by the cost difference between the two cities for each person. Send the first half to A and the second half to B.
Time complexity: O(N log N),
Space complexity: O(1).
1 | class Solution { |
1031. Maximum Sum of Two Non-Overlapping Subarrays
Intuition:
Somewhat brute-force: compute the sums of all subarrays of length L and M, then pick the maximum non-overlapping pair.
Time complexity: O(N ^ 2)
Space complexity: O(N)
1 | class Solution { |
The one-pass solution uses DP.
Time complexity: O(N),
Space complexity: O(1).
1 | class Solution { |
1032. Stream of Characters
Intuition:
A Trie can solve this kind of string matching query problem very well.
Matching from back to front is more intuitive, slightly better in time, and simpler to implement.
Time complexity: construction O(words.length * word.length), query O(word.length).
Space complexity: worst case O(words.length * word.length).
1 | class StreamChecker { |
Afterword
It has been two and a half months since a senior from Kuaishou asked me about new C++ features during Spring Festival. During this period, I read C++ Primer, and I am currently reading Effective Modern C++ and C++ Concurrency in Action. I am confident that I understand the new features better now, and I am also trying to participate more in C++ projects and develop C++ as my main language. I hope this can become one of my core competitive advantages when I look for a job next year.