LeetCode weekly contest 150
| Rank | Name | Score | Finish Time | Q1 (3) | Q2 (4) | Q3 (5) | Q4 (7) |
|---|---|---|---|---|---|---|---|
| 476 / 5091 | YoungForest | 19 | 1:31:13 | 0:03:52 | 0:09:23 | 1:16:13 2 | 0:50:16 1 |
The result of this contest was poor again, with a rank in the 400s. The direct reason was that I spent too much time on search pruning for the third problem and did not get it right in one shot. The root cause was that I finally had a two-week holiday at home recently, relaxed my practice, and basically stopped grinding problems; naturally, my hands got rusty. It really matches the saying, “Excellence comes from diligence and is ruined by idleness.” Last week’s rank was also very bad.
1160. Find Words That Can Be Formed by Characters
A standard letter-counting problem. Store counts with a hashmap. One detail to note is that when checking each word, you need to get a copy of the counts.
Time complexity: O(chars.size() + words.size() * word.size()),
Space complexity: O(26)
1 | class Solution { |
1161. Maximum Level Sum of a Binary Tree
First get the sum of each level, then take the maximum.
Time complexity: O(N),
Space complexity: O(N).
1 | /** |
1162. As Far from Land as Possible
Start from land and run DFS.
The pruning condition is quite complicated. Pay attention to:((src_i == i && src_j == j) || (direct_i == di[k]) || (direct_j == dj[k]) || (direct_i == 0 && dj[k] == 0) || (direct_j == 0 && di[k] == 0)) && grid[ni][nj] != 1.
These mean:
- The search direction is consistent with the previous direction
- The current starting point is the origin
- The row direction is consistent
- The column direction is consistent
- Previously we did not move in the row direction, and now we move in the row direction
- Previously we did not move in the column direction, and now we move in the column direction
- It is not land
Time complexity: O((rowcolumn) ^ 2),
Space complexity: O(rowcolumn).
In fact, the better algorithm is BFS. Initialize the queue with all land cells. BFS guarantees that each cell is visited only once.
1 | class Solution { |
1163. Last Substring in Lexicographical Order
The answer may start from some character and continue to the end of the string.
The first character must be the largest character.
Each time, select the largest character, then select the second character, and keep moving backward until only one candidate remains.
One pruning case is something like 'zzzz'. Everyone has the largest character, so the only candidate should be the first largest character.
Time complexity: O(N). Because of the pruning condition, the number of candidates is N + N / 2 + N / 4 + ... + 1 = 2 * N.
Space complexity: O(N).
1 | class Solution { |
Afterword
I once wrote a very careful answer on Zhihu: How does everyone grind LeetCode?. Over the past few months, students have kept following me, and some even commented that they would keep watching what I do next. I should not let them down. Time to clean up this lazy mood and face the new semester in full grind-mode!