LeetCode Biweekly Contest 47
| Rank | Name | Score | Finish Time | Q1 (3) | Q2 (4) | Q3 (5) | Q4 (6) |
|---|---|---|---|---|---|---|---|
| 64 / 9933 | YoungForest | 18 | 0:55:55 | 0:03:37 | 0:07:16 | 0:13:28 | 0:55:55 |
A crazy rating-gain round. I solved 3 problems in 13 minutes. The idea for the last problem was also relatively smooth. I hit two blockers: first, at the beginning I forgot to consider point pairs with no edge between them; second, I solved the complementary problem, but when returning the answer, I carelessly thought the total count was n^2, while in fact it is C(n, 2) = n * (n - 1) / 2. Debugging wasted quite a bit of time. If it had gone more smoothly, maybe the result could have broken through the sky and won the generous top-20 prize.
1779. Find Nearest Point That Has the Same X or Y Coordinate
Warm-up problem. Traverse once, checking whether a point is in the same row/column and whether the distance is closer.
1 | class Solution { |
Time complexity: O(N),
space complexity: O(1).
1780. Check if Number is a Sum of Powers of Three
Ternary encoding. Each digit can only be 0 or 1, not 2.
1 | class Solution { |
Time complexity: O(log_3 N),
space complexity: O(1).
1781. Sum of Beauty of All Substrings
Observing that the constraint is not large, 500, we can brute-force enumerate all substrings and update beauty in O(1).
1 | class Solution { |
Time complexity: O(N^2 * log 26),
space complexity: O(26).
1782. Count Pairs Of Nodes
Observe that the number of vertices V is still large: 2*10^4, so we cannot enumerate all point pairs.
But the number of edges E is not large, 10^5, so this is not a dense graph.
Therefore, we can brute-force over edges.
Also, queries.size() is small, only 20.
The overall idea is to first compute every node’s degree. If the sum of two nodes’ degrees is less than or equal to query, then the cnt of the two nodes must be less than or equal to query. This is because cnt(a, b) <= degree(a) + degree(b), where equality holds if and only if a and b are not directly connected.
We can first sort the degrees, then use a two sum style binary search to find all point pairs satisfying degree(a) + degree(b) <= query.
Although the problem asks for cnt strictly greater than query, we first compute cnt <= query, because the two are complements. In addition to the point pairs above, some point pairs with edges may also satisfy the requirement. These pairs are:degree(a) + degree(b) > query and degree(a) + degree(b) - edges(a, b) <= query. We only need to traverse all edges once.
1 | class Solution { |
Time complexity: O(E + queries.size() * (E + V log V)),
space complexity: O(V + E).