Skip to content

Commit e0fdfae

Browse files
Solve Leetcode
- 1351. Count Negative Numbers in a Sorted Matrix.
1 parent 5b3d060 commit e0fdfae

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
`1351` `Easy`
2+
3+
# Count Negative Numbers in a Sorted Matrix
4+
5+
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.
6+
7+
## Example
8+
9+
```
10+
Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
11+
Output: 8
12+
Explanation: There are 8 negatives number in the matrix.
13+
```
14+
15+
## Example
16+
17+
```
18+
Input: grid = [[3,2],[1,0]]
19+
Output: 0
20+
```
21+
22+
## Constraints:
23+
24+
- m == grid.length
25+
- n == grid[i].length
26+
- 1 <= m, n <= 100
27+
- -100 <= grid[i][j] <= 100
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int countNegatives(int[][] grid) {
3+
int countNegative =0;
4+
int rows = grid.length;
5+
for(int i=0;i<rows;i++){
6+
int cols = grid[i].length-1;
7+
for(int j=cols;j>=0;j--){
8+
if(grid[i][j]<0) countNegative++;
9+
}
10+
}
11+
return countNegative;
12+
}
13+
}

LeetCode/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- 2206 Divide Array Into Equal Pairs
1212
- 977 Squares of a Sorted Array
1313
- 905 Sort Array By Parity
14+
- 1351 Count Negative Numbers in a Sorted Matrix
1415

1516
## Medium
1617
- 73 Set Matrix Zero

0 commit comments

Comments
 (0)