-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCountNegativeNumbersInASortedMatrix.java
More file actions
40 lines (40 loc) · 1.03 KB
/
CountNegativeNumbersInASortedMatrix.java
File metadata and controls
40 lines (40 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package io.ziheng.array.leetcode;
/**
* LeetCode 1351. Count Negative Numbers in a Sorted Matrix
* https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/
*/
public class CountNegativeNumbersInASortedMatrix {
private int cnt = 0;
public int countNegatives(int[][] grid) {
if (grid == null) {
return 0;
}
countNegatives0(0, 0, grid);
return cnt;
}
private void countNegatives0(int x, int y, int[][] grid) {
/**
* [
* [4,3,2,-1],
* [3,2,1,-1],
* [1,1,-1,-2],
* [-1,-1,-2,-3]
* ]
*/
if (x >= grid.length || y >= grid[0].length) {
return;
}
for (int i = x; i < grid[0].length; i++) {
if (grid[x][i] < 0) {
cnt++;
}
}
for (int i = x + 1; i < grid.length; i++) {
if (grid[i][y] < 0) {
cnt++;
}
}
countNegatives0(x + 1, y + 1, grid);
}
}
/* EOF */