LeetCode Weekly Contest 239
| Rank | Name | Score | Finish Time | Q1 (3) | Q2 (5) | Q3 (5) | Q4 (6) |
|---|---|---|---|---|---|---|---|
| 78 / 10870 | YoungForest | 18 | 0:52:57 | 0:02:22 | 0:09:37 | 0:27:13 | 0:47:57 1 |
I have been exempt from check-ins for three consecutive weeks, and last night’s biweekly contest also went pretty well as a hand-speed contest.
The recent weekly contests have indeed become somewhat easier. It seems I am still more suited to easy problems. Hard+ problems are still not quite my thing.
1848. Minimum Distance to the Target Element
A warm-up problem.
One pass, recording the best answer.
1 | class Solution { |
1849. Splitting a String Into Descending Consecutive Values
Backtracking, searching possible split points.
1 | class Solution: |
Time complexity: O(N^2),
space complexity: O(N).
Although we used backtracking, which looks like it should have high time complexity, O(N ^ N), in fact in the for loop inside dfs, there is only one possible path that satisfies the condition and can dfs to the next layer.
Refer to this solution’s analysis.
1850. Minimum Adjacent Swaps to Reach the Kth Smallest Number
First, we need to review LC 31. Next Permutation and learn how to solve the next enumerated permutation. Fortunately, C++ STL already provides the std::next_permutation function, which can be used directly. Its amortized time complexity is O(1).
Use next_permutation k times to find the Kth Smallest Number.
After that, just use a greedy idea to find the minimum number of swaps.
That is, whenever a position differs, find the nearest equal digit behind it and move it to the current position through swaps.
1 | class Solution { |
Time complexity: O(k + n^2),
space complexity: O(n).
1851. Minimum Interval to Include Each Query
This problem is somewhat similar to the last problem from last night’s biweekly contest, and it needs the offline query technique.
Sort intervals by size from small to large. Whenever a new interval is added, update the answers for the queries it can cover.
Here we need to use multimap to maintain unresolved queries, because it supports binary search, deletion, and duplicate queries.
1 | class Solution { |
n = intervals.length, m = queries.length
Time complexity: O(n log n + m log m + n log m),
space complexity: O(m).