+[The matrix is sorted in non-increasing order both row-wise (left to right decreases) and column-wise (top to bottom decreases). I need to count negatives quickly. A naive approach is to check every cell O(m*n). Since rows are sorted, in each row I could binary search the first negative index, giving O(m log n). But the follow-up asks for O(m + n). I recall a standard trick: start at the top-right corner. If that element is negative, then everything below in that column must be negative (because column is non-increasing downwards), so I can add (m - i) and move left one column. If it's non-negative, move down to next row. That walks at most m + n steps. This should be simple and constant extra space.]
0 commit comments