Count Negatives in Sorted 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:

8 negatives.

Input:grid = [[1]]
Output:0
Explanation:

The only element is 1, which is not negative, so the count is 0.

Input:grid = [[3,2],[1,0],[-1,-2]]
Output:2
Explanation:

There are 2 negative numbers in the matrix: -1 and -2. The matrix is sorted in descending order both row-wise and column-wise, with negatives appearing at the bottom-right portion.

Constraints

  • 1 ≤ m, n ≤ 100

Ready to solve this problem?

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