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; } };
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