Description
Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.
Examples
Input:
grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]Output:
8Explanation:
Row 0 [4,3,2,-1]: 1 negative (-1). Row 1 [3,2,1,-1]: 1 negative (-1). Row 2 [1,1,-1,-2]: 2 negatives (-1,-2). Row 3 [-1,-1,-2,-3]: 4 negatives. Total: 1+1+2+4 = 8.
Input:
grid = [[3,2],[1,0]]Output:
0Explanation:
All values in the matrix are non-negative, so the count of negative numbers is 0.
Input:
grid = [[-1,-2,-3],[-2,-3,-4],[-3,-4,-5]]Output:
9Explanation:
All numbers in the matrix are negative, so all 9 elements are counted (3 rows x 3 columns).
Constraints
- •
1 ≤ m, n ≤ 100