LeetCode weekly contest 125
This weekend was insanely busy, and I paid the price for my procrastination and laziness. Everything piled up together, and Sunday had a ridiculous number of deadlines. At 10 a.m. I had a meeting with my advisor to discuss how to prepare the make-up exam for the undergraduate Computer Organization course. By the time I got back to the lab after the meeting, the contest had already started. After a brief hesitation over whether to join the contest as planned or first finish preparing the make-up exam, I started this week’s weekly contest. It is one of the few things I have kept doing over the past two months, and continuing it is no longer only about improving my algorithm skills. It is also a huge encouragement for my confidence in taking control of my own life.
This week’s weekly contest was relatively easy. Although I was 10 minutes late, I still finished all four problems 30 minutes early. In particular, the fourth problem was worth 8 points and marked Hard, but I solved it quickly. I think its difficulty was at most Medium.
Result:
| Rank | Name | Score | Finish Time | Q1 (4) | Q2 (4) | Q3 (6) | Q4 (8) |
|---|---|---|---|---|---|---|---|
| 284 / 4238 | YoungForest | 22 | 1:08:40 | 0:18:40 | 0:31:07 WA(1) | 0:39:39 | 1:03:40 |
997. Find the Town Judge
Use two hashmaps: one records how many people each person trusts, and the other records how many people trust each person.
1 | class Solution { |
999. Available Captures for Rook
This fully matches Easy difficulty. Directly simulate the rook, whose movement should be similar to the rook in Chinese chess, walking step by step in four directions.
In the words of someone from Discuss: search and capture.
1 | class Solution { |
998. Maximum Binary Tree II
The key to this problem is understanding what the so-called Maximum Binary Tree means.
Its definition itself is recursive: given an array A, the maximum value in A is the root node; the subarray to the left of the maximum value forms the left subtree, and the subarray to the right forms the right subtree.
This problem gives a Maximum Binary Tree but does not tell you the original array. It then gives one more value and asks for the Maximum Binary Tree constructed after appending this value to the original array.
1 | /** |
1001. Grid Illumination
Wow! LeetCode problem numbers have passed 1000. Although the total number of problems is still under 1k, reaching it is only a matter of time.
There are 949 algorithm problems now, and I have only done 210. Come on, Forest!
The idea for this problem is very direct. Once you understand the statement, just search. The key is search efficiency.
From the constraints:
1 <= N <= 10^9
0 <= lamps.length <= 20000
0 <= queries.length <= 20000
lamps[i].length == queries[i].length == 2
we know that search is best implemented with a hashtable for maximum efficiency. However, C++’s default pair is not hashable. Although implementing a hashable pair yourself can further optimize efficiency, I was lazy here and directly used set instead of unordered_set. It still ACed. So happy. I finished the contest 30 minutes early, and my rank returned to the top 300 again. Keep it up and stay within the top 300.
1 | class Solution { |
Time complexity: O(max(M log N, N log N)), where N = lamps.size() and M = queries.size(). If using a hashable pair, the complexity can be further reduced to O(max(N, M)).