Problem: Count Negative Numbers in a Sorted Matrix
Medium
30 min
Explore methods to count negative numbers in a matrix sorted in non-increasing order by rows and columns. Understand constraints and apply efficient traversal strategies to solve this common matrix problem in coding interviews.
Statement
Given a matrix grid of size m∗n, where each row and column is sorted in non-increasing order, find and return the total count of negative numbers in the matrix.
Constraints:
m==grid.length
n==grid[i].length
1≤m, n≤1000
−100≤grid[i][j]≤100
Examples
1/ 3
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Count negative numbers in a sorted matrix
1.
grid = [[5, 2, -1], [4, 1, -2], [-1, -3, -4]]
How many negative numbers are there in the matrix?
A.
3
B.
4
C.
5
D.
6
1 / 3
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.
1
2
3
4
5
Try it yourself
Implement your solution in the following coding playground:
Problem: Count Negative Numbers in a Sorted Matrix
Medium
30 min
Explore methods to count negative numbers in a matrix sorted in non-increasing order by rows and columns. Understand constraints and apply efficient traversal strategies to solve this common matrix problem in coding interviews.
Statement
Given a matrix grid of size m∗n, where each row and column is sorted in non-increasing order, find and return the total count of negative numbers in the matrix.
Constraints:
m==grid.length
n==grid[i].length
1≤m, n≤1000
−100≤grid[i][j]≤100
Examples
1/ 3
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Count negative numbers in a sorted matrix
1.
grid = [[5, 2, -1], [4, 1, -2], [-1, -3, -4]]
How many negative numbers are there in the matrix?
A.
3
B.
4
C.
5
D.
6
1 / 3
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.
1
2
3
4
5
Try it yourself
Implement your solution in the following coding playground: