[LeetCode] Count Negative Numbers in a Sorted Matrix

1351. Count Negative Numbers in a Sorted Matrix

Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int res = 0;
for(auto& r : grid) {
res += lower_bound(rbegin(r), rend(r), 0) - rbegin(r);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/08/PS/LeetCode/count-negative-numbers-in-a-sorted-matrix/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.