Count Negative Numbers in Matrix

Easy

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:8
Explanation:

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:0
Explanation:

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:9
Explanation:

All numbers in the matrix are negative, so all 9 elements are counted (3 rows x 3 columns).

Constraints

  • 1 ≤ m, n ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!