LeetCode Weekly Contest 213
| Rank | Name | Score | Finish Time | Q1 (3) | Q2 (4) | Q3 (5) | Q4 (6) |
|---|---|---|---|---|---|---|---|
| 139 / 10630 | YoungForest | 18 | 0:39:13 | 0:06:26 | 0:14:56 | 0:22:59 | 0:39:13 |
I have performed well in weekly contests for four consecutive weeks. Happy. My group ranking has also stabilized at 15th. In the end, I still cannot enter the top 10, but I am already satisfied.
5554. Check Array Formation Through Concatenation
Warm-up problem. Note that both arr and pieces are distinct.
So we only need to record the array corresponding to the first element of each piece, look it up directly, and match.
1 | class Solution { |
Time complexity: O(arr.size() + pieces.size()),
space complexity: O(pieces.size()).
5555. Count Sorted Vowel Strings
DP. Define dp(n, i) as the number of strings of length n whose first character is the i-th largest letter.dp(n, i) = sum(dp(n-1, j) for j in range(i, 0, -1)).
1 | class Solution: |
Time complexity: O(n * 5 * 5),
space complexity: O(n * 5).
Of course, there is also zerotrac’s O(1) solution in the comments. Too strong. Anyway, I did not understand it.
5556. Furthest Building You Can Reach
Greedy. Use ladders as much as possible on the largest height differences.
Here we need a priority_queue to maintain the largest previous height differences.
1 | class Solution { |
5600. Kth Smallest Instructions
Two weeks ago, a Hulu interview asked me about an algorithm for finding the k-th largest number in a binary search tree. That was also one of the implementations I first saw in Algorithms, 4th Edition. In regular problem solving, I also often need this kind of rank-aware data structure.
This problem is very similar to the idea of finding rank: maintain the size of a subtree, then choose the walking decision based on the relative size of the “left subtree, meaning going right” and the rank. The difference is that in this problem, the subtree size is the number of possible paths, which is a combination count.
This problem uses zerotrac’s method of computing combinations with Pascal’s triangle. Compared with a factorial algorithm, it supports modulo and also avoids overflow.
1 | class Solution { |
Time complexity: O((rows + cols) * cols),
space complexity: O((rows + cols) * cols).