Skip to content

Commit e4cd85e

Browse files
authored
Create count-negative-numbers-in-a-sorted-matrix.cpp
1 parent e0816ea commit e4cd85e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time: O(m + n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int countNegatives(vector<vector<int>>& grid) {
7+
int result = 0, c = grid[0].size() - 1;
8+
for (const auto& row : grid) {
9+
while (c >= 0 && row[c] < 0) {
10+
--c;
11+
}
12+
result += grid[0].size() - 1 - c;
13+
}
14+
return result;
15+
}
16+
};

0 commit comments

Comments
 (0)