This contest went rather badly. My rank was roughly 1486 / 3845. The problems were:
The second problem was relatively simple. Since it was an Easy problem, I directly used brute force and got one TLE. Earlier, because of carelessness, I also wrote the wrong variable in the for loop condition once. That caused two penalties.
The third problem was not very hard either, but in the end I did not think of the O(n) solution. I only thought of an O(n ^ 2) approach. I thought of prefix sums and noticed the keyword divisible, but I did not connect it to the key point that equal prefix sums work.
For the fourth problem, I thought of DP. I got stuck on the step of “finding the number just slightly larger in the later array”, that is, I did not think of using TreeMap. Fundamentally, this is because I am not familiar enough with basic data structures.
classSolution { public: vector<vector<int>> kClosest(vector<vector<int>>& points, int K) { map<int, vector<vector<int>>> distance; for (constauto point: points) { distance[point[0]*point[0] + point[1]*point[1]].push_back(point); } vector<vector<int>> results; int i = 0; for (constauto d: distance) { for (constauto point: d.second) { results.push_back(point); i++; if (i >= K) { return results; } } } return results; } };
976. Largest Perimeter Triangle
Just find the largest three numbers that can form a triangle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: intlargestPerimeter(vector<int>& A){ int result = 0; sort(A.begin(), A.end()); for (int i = A.size() - 3; i >= 0; i--) { if (A[i] + A[i+1] > A[i+2]) { result = A[i] + A[i+1] + A[i+2]; break; } } return result; } };
974. Subarray Sums Divisible by K
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intsubarraysDivByK(vector<int>& A, int K){ unordered_map<int, int> prefix_sum; int sum = 0; int result = 0; prefix_sum[0] = 1; for (int i = 0; i < A.size(); i++) { sum = (sum + A[i] % K + K) % K; result += prefix_sum[sum]; prefix_sum[sum]++; } return result; } };
975. Odd Even Jump
In C++, the default implementation of map is a Tree Map.