LeetCode weekly contest 128
The first three problems went smoothly and were solved within 30 minutes. For the last Hard problem, my thinking was quite messy, and even after one hour I still did not solve it.
This contest made me feel that it still comes down to familiarity.
Because I had done similar problems before for problems 2 and 3, I solved them quickly. The second problem even took only 2 minutes!!!
| Rank | Name | Score | Finish Time | Q1 (2) | Q2 (4) | Q3 (6) | Q4 (8) |
|---|---|---|---|---|---|---|---|
| 348 / 5164 | YoungForest | 19 | 1:24:03 | 0:11:33(1) | 0:13:42(1) | 0:27:37 | None |
1012. Complement of Base 10 Integer
1 | class Solution { |
Time complexity: O(log N), proportional to the number of digits.
Space complexity: O(1).
Because I did not notice the corner case if (N == 0) return 1;, I got one wrong answer.
My method is straightforward, with no special technique or trick. After all, it is a warm-up problem, and the time complexity is sufficient.
There are also some other ideas:
for example, find the largest X = 1111..11 such that X >= N, then simply return X - N or return X^N.
1013. Pairs of Songs With Total Durations Divisible by 60
1 | class Solution { |
This uses the same idea as the famous two sum, testing the use of a hashmap.
To map all values, including negative numbers, into the range under 60, I used (60 - t + 60000) % 60. A better approach is actually (60 - t % 60) % 60.
Time complexity: O(N),
space complexity: O(N).
1014. Capacity To Ship Packages Within D Days
A typical binary search problem. I also encountered one like this while helping my senior with ByteDance’s written test over the weekend. The idea was clear, and writing it felt quite practiced.
1 | class Solution { |
Time complexity: 50000 * log(50000 * 500),
space: O(1).
1015. Numbers With 1 Repeated Digit
I spent an hour on it and still did not solve it. Looking back, the correct idea is to count how many numbers have no repeated digits, and divide the number set into 0, 1 through digit-1, and digit. The wrong ideas were: not using permutation to simplify the counting; not analyzing the number patterns thoroughly enough. In fact, it only needs to be divided into two categories: 0 to digit-1, and digit. Because of these mistakes, although I could calculate the count by hand, the code implementation became too complex and too long, making it very hard to get right directly. In the end, I had to give up.
I referred to the solutions from lee215 and heqingy.
1 | class Solution { |
Time complexity: O(log n), the number of digits.
Space complexity: O(1).
Postscript
I have solved 279 problems now. I hope that after solving more problems, I can reach the state where, when I see a problem, I have an idea; once I have an idea, I can write it down from memory, bug-free. How many problems does it take to reach that state? 600? 900?
No matter how many it takes, I have to keep going.
I used to have a misunderstanding about grinding problems: I was not fast enough, and I did not set aside enough focused time every day. Only when the speed is high can it really be called grinding.
Let me clarify my goal again: spend one year preparing the skills needed for interviews. Data structures and algorithms are the foundation. For the rest, including programming languages, computer architecture, operating systems, design patterns, compilers, and networking, I also need to fill them in slowly. Around this time next year, it will be time to go into battle for real.
Keep going, Forest!