1476. Count Negative Numbers in a Sorted Matrix
My accepted C++ solution to LeetCode problem 1476, Count Negative Numbers in a Sorted Matrix, running in 0ms.
- Difficulty: Easy
- C++
- Runtime 0ms
- Memory 14.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
C++
Accepted on LeetCode — runtime 0ms, memory 14.2MB, accepted 2025-12-28.
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
int count = 0, col = n - 1;
for (int row = 0; row < m; row++) {
while (col >= 0 && grid[row][col] < 0) col--;
count += n - col - 1;
}
return count;
}
};