I finally returned to my dear motherland. I can get up on Sunday morning to play LeetCode contests again. I have to admit that after four months away, I became much rustier. The ranking says it all. This contest was purely a speed contest, and speed depends heavily on form and familiarity. So I fully accept the drop in ranking. It also happens to push me to put more effort into preparing for the upcoming Google interview.
1341. The K Weakest Rows in a Matrix
Use binary search to find the number of soldiers in each row, then sort to find the first K rows with the fewest soldiers.
Time complexity: O(m log m * log n) Space complexity: O(m)
classSolution { public: intminSetSize(vector<int>& arr){ unordered_map<int, int> count; for (int i : arr) { ++count[i]; } vector<int> v; for (auto p : count) { v.push_back(p.second); } sort(v.begin(), v.end(), greater<int>()); int ans = 0, target = (arr.size() + 1) / 2, accu = 0; while (accu < target) { accu += v[ans]; ++ans; } return ans; } };
1343. Maximum Product of Splitted Binary Tree
Enumerate and search directly. Try every subtree combination and find the maximum product. Here we need to pay attention to the modulo 10^9 + 7 operation. I got WA twice on this point. I used unsigned long long, and applied mod only at the end.