LeetCode weekly contest 123
This contest was the first one after the Spring Festival.
989. Add to Array-Form of Integer
Idea: simulate the written addition process and add digit by digit. The official Solution has a very vivid name for it: Schoolbook Addition.
Time complexity: O(max(N, M)), where N and M are the lengths of A and K, respectively.
Space complexity: O(M-N), namely the space used by the deque.
1 | class Solution { |
990. Satisfiability of Equality Equations
Idea: use Union-Find to store equality relationships, then iterate over all inequality relationships and check whether they are between different sets.
Time complexity: O(N),
space complexity: O(N).
1 | class Solution { |
991. Broken Calculator
Idea: once X is greater than Y, we can only reach Y by decrementing.
If X is smaller than Y, we can either DOUBLE or Decrement.
How should we choose then? We can observe that Double, Decrement, Decrement and Decrement, Double produce the same result.
From this, we can see:
If Y is odd, it must be produced by Double, Decrement.
If Y is even, it must be produced by Double (using Decrement takes more operations).
Time complexity: O(log Y);
space complexity: O(log Y), because of recursion.
1 | class Solution { |
992. Subarrays with K Different Integers
I had 40 minutes left for the last problem. I aimed to solve this Hard problem.
Idea: based on the problem, the time complexity looks like O(n ^ 2), since we need to traverse all subarrays at least once.
If we use two loops to enumerate all subarrays, how can we quickly obtain the number of distinct numbers in each subarray?
Dynamic programming can be used to reduce complexity.
For each subarray’s distinct numbers, we use hashmap<int, int>, where key represents a distinct number and value represents its frequency.
Let f(i, j) be the hashmap corresponding to the distinct numbers in subarray [i, j].
Then:f(i, j) =f(i, j-1).insert(A[j])
orf(i+1, j).delete(A[i])new(A[i]), if i == j.
If we build the hashmap bottom-up, the outer loop is new and the inner loop is delete.
The result was TLE.
Think about whether there is an O(n log n) solution.
1 | class Solution { |
In the end, I still did not come up with a further optimized algorithm. After reading the Solution, I was surprised to find that there is an O(N) solution. My observation of the properties of the number of distinct values in subarrays was still not careful enough.
First, if we fix the right boundary, then for two subarrays (i1, j) and (i2, j), where i1 < i2, we have f(i1, j) <= f(i2, j).
Therefore, for each right boundary j, the left boundary of qualifying subarrays lies within a certain range, which can be written as [left1, left2].
Second, as we grow the right boundary, if the number of distinct values exceeds K, we need to grow left1 and left2. In other words, left1 and left2 increase monotonically with the right boundary.
1 | class Solution { |
Postscript
Today is one of my last three days at home. As expected, going home for winter vacation mostly wastes time. Staying at school in Beijing, together with like-minded classmates, gives me stronger motivation to study. The pressure of competition is also greater. No wonder everyone longs for a better environment and better friends.
Keep going, Forest!