LeetCode Weekly Contest 181
Since LeetCode updated its rating algorithm, my rating reached its peak and has been dropping ever since. But that is also because I am not good enough; every contest I rank several hundred places down, and occasionally over a thousand.
| Rank | Name | Score | Finish Time | Q1 (3) | Q2 (4) | Q3 (5) | Q4 (6) |
|---|---|---|---|---|---|---|---|
| 839 / 10930 | YoungForest | 18 | 1:31:13 | 0:04:53 | 0:14:43 1 | 0:45:27 1 | 1:16:13 1 |
1389. Create Target Array in the Given Order
A warm-up problem. Use vector‘s insert interface. The downside is efficiency, but it is enough for a warm-up problem.
Time complexity: O(n ^ 2), in the worst case, every insertion is at the front.
Space complexity: O(n).
1 | class Solution { |
1390. Four Divisors
Brute force. Calculate the divisors of each number.
Time complexity: O(sqrt(nums[i]) * nums.length),
space complexity: O(nums.length) -> O(1) without memo.
1 | class Solution { |
1391. Check if There is a Valid Path in a Grid
This problem is not very difficult algorithmically; one search is enough. The difficulty lies in implementation and abstracting a complex problem. Here I used multiple HashMaps to solve the abstraction of directions.
Time complexity: O(row * col),
space complexity: O(1).
This problem can also be solved with Union-Find, which is a bit simpler to implement.
1 | class Solution { |
1392. Longest Happy Prefix
I referred to the code on GeeksforGeeks. It is not hard to find; Google “longest prefix suffix” and it comes up.
Time complexity: O(N),
space complexity: O(N).
1 | class Solution { |