30:00
You are given an m x n matrix grid whose rows and columns are both sorted in non-increasing order. Return how many of its numbers are negative.
m x n
grid
m==grid.lengthm == \text{grid.length}m==grid.length
n==grid[i].lengthn == \text{grid}[i].\text{length}n==grid[i].length
1≤m,n≤1001 \leq m, n \leq 1001≤m,n≤100
−100≤grid[i][j]≤100-100 \leq \text{grid}[i][j] \leq 100−100≤grid[i][j]≤100
Run code to see results.
Start at the bottom-left corner (row 3, col 0). Moving up makes values bigger and moving right makes them smaller, so each look rules out a whole row or column.
staircase walk from the bottom-left corner
c
1
2
3
0
+0
r→
pointer (r, c)
counted negative
ruled out (>= 0)
pointer
(3, 0) = -1
count
A grid has every row and every column sorted in non-increasing order. Count how many of its values are negative.