-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCountSubIslands.java
72 lines (59 loc) · 1.89 KB
/
CountSubIslands.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package leetcode.solution.DFS;
/**
* 1905. Count Sub Islands
*/
public class CountSubIslands {
public static void main(String[] args) {
int[][] grid1 = {{1, 1, 1, 0, 0}, {0, 1, 1, 1, 1}, {0, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {1, 1, 0, 1, 1}};
int[][] grid2 = {{1, 1, 1, 0, 0}, {0, 0, 1, 1, 1}, {0, 1, 0, 0, 0}, {1, 0, 1, 1, 0}, {0, 1, 0, 1, 0}};
CountSubIslands numberOfIslands = new CountSubIslands();
int ans = numberOfIslands.countSubIslands(grid1, grid2);
System.out.println(ans);
// 3
}
private int[][] data2;
/**
* four directions
*/
private int[][] direct;
public int countSubIslands(int[][] grid1, int[][] grid2) {
data2 = grid2;
int m = grid1.length;
int n = grid1[0].length;
this.direct = new int[][]{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
// exclude islands that are not sub-island,
// which means that for some i、j, grid2 == land but grid1 is water
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (data2[i][j] == 1 && grid1[i][j] == 0) {
helper(i, j);
}
}
}
// count and remove island, same as '200'
int count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (data2[i][j] == 1) {
count++;
helper(i, j);
}
}
}
return count;
}
private void helper(int i, int j) {
if (i < 0 || j < 0 || i >= data2.length || j >= data2[0].length) {
return;
}
if (data2[i][j] == 0) {
return;
}
data2[i][j] = 0;
for (int[] item : direct) {
int nextI = item[0] + i;
int nextJ = item[1] + j;
helper(nextI, nextJ);
}
}
}