Skip to content

Count Negative Numbers in a Sorted Matrix

EasyBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.

You are given an m x n grid grid that is sorted in non-increasing order both along each row and down each column. Return the number of negative values in the grid. Can you do it in O(m + n)?

grid = [[4, 3, 2, -1],
        [3, 2, 1, -1],
        [1, 1, -1, -2],
        [-1, -1, -2, -3]]   →  8
 
grid = [[3, 2], [1, 0]]     →  0

Constraints

  • 1 <= m, n <= 100
  • -100 <= grid[i][j] <= 100