LeetCode weekly contest 120
This week, I joined the weekly contest together with my good friend “Female Voice Male”. Competing with a classmate still brings quite a bit of pressure. I have been practicing algorithm problems for half a year, while he is still a beginner. If I lost in the end, that would be embarrassing. Fortunately, the result was acceptable, and I did not embarrass myself. I ACed all problems with 10 minutes left, and every problem passed on the first try, so I was slightly ahead. I have to say that this contest’s problems were much easier than previous ones. My previous level had stayed at solving only two problems with a ranking around 800, while this time my ranking was 356 / 3870. From the ranking, there was some progress.
Below I share the ideas for the four problems.
977. Squares of a Sorted Array
Given an array, return the squared values of its elements, sorted. Direct brute force is enough.
1 | class Solution { |
Time complexity: O(n log n), because there is a sort.
Space complexity: O(n), because a new array needs to be returned. Other than that, no extra space is used. Of course, you can also use the space occupied by the input array, which is trickier but not very meaningful.
After reading the Solution, I was surprised to find that there is an O(n) approach. Looking carefully, I realized I had not noticed that array A itself is already sorted. The problem can be transformed into merging two sorted arrays.
1 | class Solution { |
978. Longest Turbulent Subarray
Given an array, return the maximum length of a “turbulent subarray”. A “turbulent subarray” means that the sign of each adjacent pair’s comparison alternates, such as [1, 2, 1, 2, 1]. Plotted on a coordinate chart, it looks like a wave.
Idea: traverse the array once. If the comparison sign of the new adjacent pair is opposite to the previous pair’s sign, then current turbulent subarray length += 1; otherwise, reset current turbulent subarray length to 2. During traversal, update longest turbulent subarray length according to current turbulent subarray length.
1 | class Solution { |
Time complexity: O(n), only one traversal of the array is needed;
space complexity: O(1), only constant extra space is used.
979. Distribute Coins in Binary Tree
Intuition: when encountering a tree problem, the first thing to think of is recursion and DFS. The hardest part of writing DFS directly is determining the parameters and return value, that is, clarifying the relationship between the subtree and the root. In this problem, the relationship between a subtree and the root is coin transfer, either from the subtree to the root or from the root to the subtree. So only one return value is needed, representing this transfer.
1 | /** |
Time complexity: O(n), where n is the number of nodes, because each node is called once.
Space complexity: average O(log n), namely the tree depth.
980. Unique Paths III
Intuition: to find the number of paths, the most direct brute-force approach is DFS with backtracking. Since the problem scale is small, 1 <= grid.length * grid[0].length <= 20, it can still AC. The exciting part was that I ACed this last problem with 10 minutes left before the contest ended.
1 | class Solution { |
Time complexity: O(4^n), where n is the number of grid cells, because each step can go in four directions and backtracking is necessarily exponential.
Space complexity: O(n), the deepest recursion depth of DFS.