Count Negative Numbers in a Sorted Matrix LeetCode solution in Java
1class Solution {2public int countNegatives(int[][] grid) {3int m = grid.length;4int n = grid[0].length;5int c = 0;6int count = 0;7int r = m - 1;8while (r >= 0 && c < n) {9if (grid[r][c] < 0) {10r--;11count += n - c;12} else {13c++;14}15}16return count;17}18}