LeetCode weekly contest 127
The scores for this week’s four problems were 4, 4, 5, and 6, so they should not be too hard. Keep going, Forest!
Because the problems were too simple, even though I finished 15 minutes early, my rank was still 912 / 4712. This contest was really easy, and it completely tested coding speed and familiarity. Whether you can get it bug-free on the first try matters a lot. If a corner case is wrong, debugging it takes a lot of time. I got one wrong answer on both problems 1 and 2, which cost me quite a bit of time.
| Rank | Name | Score | Finish Time | Q1 (4) | Q2 (4) | Q3 (5) | Q4 (6) |
|---|---|---|---|---|---|---|---|
| 931 / 4059 | YoungForest | 19 | 1:24:03 | 0:24:49(1) | 0:44:58(1) | 1:05:12 | 1:14:03 |
1005. Maximize Sum Of Array After K Negations
A routine warm-up problem.
Intuition:
If there are negative numbers, flip the smallest negative numbers first;
if there are no negative numbers but there is a zero, flip zero;
if there are only positive numbers and K is odd, flip the smallest positive number;
if K is even, do not flip.
Time complexity: O(N log N), because of sorting.
Space complexity: O(1).
1 | class Solution { |
I got one Wrong Answer because when flipping the smallest positive number, I did not consider a smaller positive value that originally came from a negative number. That is why the line if (i > 0 && A[i-1] < A[i]) A[i-1] = -A[i-1]; is needed.
Even for a warm-up problem, I still found some impressive code in the Discuss section. For example, an O(N) solution.
1 | class Solution { |
Time complexity: O(N) on average.
Space complexity: O(1).
If we also want the worst case to be O(N), we can use randomized partition.
1006. Clumsy Factorial
In my first submission, I misunderstood the precedence and lowered the priority of -, then wrote a foolish recursive solution. The result was that negative times negative became positive, which was obviously wrong.
Intuition:
Compute directly according to the definition of “Clumsy Factorial”.
1 | class Solution { |
Time complexity: O(N),
space complexity: O(1).
The Discuss section had a mathematical O(1) optimization, and I have to include the original link. It is truly ridiculous in the best way.
1007. Minimum Domino Rotations For Equal Row
Intuition:
To make the whole row of A or B equal, there can be 12 final states: A is 1, A is 2, A is 3, A is 4, …, B is 6.
One pass over the dominoes gives the number of flips needed to reach these 12 states, using -1 to represent an impossible state.
1 | class Solution { |
1008. Construct Binary Search Tree from Preorder Traversal
Intuition:
Tree problems are usually solved with recursion. The key to this problem is understanding Binary Search Tree and Preorder Traversal.
1 | /** |
Actually, an O(N) solution exists. That is, we do not need to find the boundary between the left and right subtrees; we only need to keep expanding the tree.
1 | /** |
Postscript
Recently, because I have also been busy with a project at the China Institute of Water Resources, I have slackened a bit on problem-solving. I need to reflect on myself and recognize the current situation again. If I want to go to a foreign company, problem-solving really is just the basic skill. Borrowing the concept from martial arts novels, “data structures and algorithms are a programmer’s internal strength.” If I cannot even persist with something this simple, then my life will probably amount to nothing.
Plan: even though I come back exhausted every evening, I still need to spend one hour on problem-solving and CS fundamentals, half an hour running, and half an hour on English. I will check in on Douban every day, and I hope everyone will supervise me.