LeetCode weekly contest 129
Last weekend, because I was preparing for Google’s Kick Start Round A, I skipped one LeetCode weekly contest. But I still made up the LeetCode problems that evening. The four problems were not easy, but after thinking through them I solved them independently. It was a small comfort after Kick Start had beaten me into silence.
1020. Partition Array Into Three Parts With Equal Sum
Intuition:
One pass. Find the pivots at 1/3 and 2/3 of the total sum.
Time complexity: O(N),
space complexity: O(1).
1 | class Solution { |
1022. Smallest Integer Divisible by K
Intuition:
Simulate the long-division process.
If the last digit is not 1, 3, 7, or 9, directly return -1. That means no valid N can be found.
Then use long division: keep making the last digit equal to 1 until all preceding digits are also 1.
Since a solution must exist in that case, the loop will eventually terminate.
Time complexity: O(result.length),
space complexity: O(1).
1 | class Solution { |
1021. Best Sightseeing Pair
Intuition:
Dynamic programming. The optimal substructure is:
the maximum score of a pair ending at the i-th element equals max(pair formed by i and i-1, pair formed by i and the pair ending at i-1).
Time complexity: O(N), one pass.
Space complexity: O(1). Although the code below is implemented as O(N), the DP process only uses dp[i-1], so it can be further optimized to O(1).
1 | class Solution { |
1023. Binary String With Substrings Representing 1 To N
Intuition:
Build a table first, then query the table.
Time complexity: O(max(S.length, N)),
space complexity: O(N).
1 | class Solution { |
Postscript
I still have not organized my notes for Google Kick Start Round A. The three-hour contest was, in practice, only two hours for me. I solved the warm-up problem and the small test set of the second problem. In the last hour I had no clue at all, and I knew that even with more time I would not make a breakthrough, so I gave up. My rank was 600. It seems there was a bug in the contest system, and submissions were unavailable for the last 25 minutes; otherwise my rank might have dropped further, because many people submit another wave of code near the end.
Kick Start happens once a month. I want to continue participating in Round B next time and keep taking the beating. According to Teacher Tang, for Google campus recruiting in China, only Rounds D and E matter. I am still far away! I still need to keep grinding problems and also fill in my knowledge of competitive programming and CS. Even if I do not get into Google in the end, the job-search result should not be bad.
Keep going, Forest!