LeetCode Biweekly Contest 26
| Rank | Name | Score | Finish Time | Q1 (3) | Q2 (4) | Q3 (5) | Q4 (7) |
|---|---|---|---|---|---|---|---|
| 115 / 7795 | YoungForest | 19 | 0:33:49 | 0:03:55 | 0:08:25 | 0:11:57 | 0:28:49 1 |
The week before last was the May Day holiday, so I skipped one biweekly contest. I will still try to participate as much as possible afterward. Recently, contests have been feeling smoother and smoother, whether in terms of each contest ranking, rating, or ranking in the Cruel group. All of them have improved compared with before. In particular, I want to thank the Cruel group for its daily problem and occasional lectures, which gave me more confidence in solving DP and many Hard problems. Here I recommend the Cruel LeetCode group again.
1446. Consecutive Characters
Sliding window, maintaining a window whose characters are all equal.
Time complexity: O(N),
space complexity: O(1).
1 | class Solution { |
1447. Simplified Fractions
Enumerate all denominators and numerators, and judge whether each fraction is in simplest form. Fortunately, C++17 already provides the built-in gcd function.
Time complexity: O(n^2),
space complexity: O(n).
1 | class Solution { |
1448. Count Good Nodes in Binary Tree
DFS, passing in the maximum value among ancestor nodes as a parameter.
Time complexity: O(n),
space complexity: O(n).
1 | /** |
1449. Form Largest Integer With Digits That Add up to Target
DP. This is a knapsack problem, trying to add one digit each time.
We can use a Greedy idea for constant-factor optimization: if two numbers have the same cost, only the larger number is useful.
Time complexity: O(target * target * 9),
space complexity: O(target * target).
1 | class Solution: |
There is an O(target) solution in the Discuss. The main idea is to no longer store the largest string in dp; instead, store only the number of digits, and then construct the result backward.