In this contest I finally got AC at the last moment, which was extremely thrilling. Since joining the students in the China region, it has been hard for my weekly contest rank to enter the top 200. For example, this time I dropped from 229 to 306. I have to admit that domestic competition is fierce.
The main reason for the lower ranking was that I spent a lot of time debugging and trying things on the third problem, and almost had no time left to implement the last one. The recent lack of practice has also caused my debug ability and one-pass bug-free ability to drop sharply.
1351. Count Negative Numbers in a Sorted Matrix
Using the fact that both rows and columns are sorted, we can count in O(m + n). It is similar to search-a-2d-matrix-ii.
Time complexity: O(m + n), space complexity: O(1).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: intcountNegatives(vector<vector<int>>& grid){ int ans = 0; constint m = grid.size(); constint n = grid[0].size(); int row = 0, col = n - 1; while (row < m && col >= 0) { if (grid[row][col] < 0) { ans += m - row; --col; } else { ++row; } } return ans; } };
1352. Product of the Last K Numbers
Record the position of the last zero and the cumulative product. Determine whether the last K numbers contain a zero. If not, divide the last cumulative product by the product before the k-th number from the end.
classProductOfNumbers { vector<int> product; int zeros = 0; int index = 1; public: ProductOfNumbers() { product.push_back(1); } voidadd(int num){ if (num == 0) { product.push_back(1); zeros = index; } else product.push_back(num * product.back()); ++index; } intgetProduct(int k){ int b = index - k; int e = index; // [b, e) if (zeros < b) { return product.back() / product[product.size() - k - 1]; } else { return0; } } };
/** * Your ProductOfNumbers object will be instantiated and called as such: * ProductOfNumbers* obj = new ProductOfNumbers(); * obj->add(num); * int param_2 = obj->getProduct(k); */
1353. Maximum Number of Events That Can Be Attended
Greedy strategy. Always attend the event with the earliest ending time.
Time complexity: O(N log N), space complexity: O(N).
classSolution { public: intmaxEvents(vector<vector<int>>& events){ int ans = 0; int current_day = 1; multiset<int> q; int i = 0; sort(begin(events), end(events), [](constauto& a, constauto& b) -> bool { if (a[0] != b[0]) { return a[0] < b[0]; } else { return a[1] < b[1]; } }); while (i < events.size() || !q.empty()) { while (i < events.size() && events[i][0] <= current_day) { q.insert(events[i][1]); ++i; } while (!q.empty() && *q.begin() < current_day) { q.erase(q.begin()); } if (!q.empty() && *q.begin() >= current_day) { ++ans; // cout << *q.begin() << endl; q.erase(q.begin()); } ++current_day; } return ans; } };
1354. Construct Target Array With Multiple Sums
This problem looks hard, but once you discover that the result of each replace operation is always the largest number, you can reverse the process from the final array back to the initial array.
Time complexity: O(N log N max_number) Space complexity: O(N).