-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1020. Number of Enclaves.java
58 lines (48 loc) · 1.73 KB
/
1020. Number of Enclaves.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
class Solution {
private boolean isWater(int val) {
return val == 0;
}
public int numEnclaves(int[][] grid) {
int R = grid.length;
int C = grid[0].length;
int enclaves = 0;
// search for the boundary cells that has land==1
// LEFT BOUNDARY & RIGHT BOUNDARY
for (int row = 0; row < R; row++) {
// only 1st and last col check for land
if (!isWater(grid[row][0]))
checkIfFormsAnIsland(grid, row, 0);
if (!isWater(grid[row][C - 1]))
checkIfFormsAnIsland(grid, row, C - 1);
}
// TOP BOUNDARY & BOTTOM BOUNDARY
for (int col = 0; col < R; col++) {
// only 1st and last col check for land
if (!isWater(grid[0][col]))
checkIfFormsAnIsland(grid, 0, col);
if (!isWater(grid[R - 1][col]))
checkIfFormsAnIsland(grid, R - 1, col);
}
// now only count for no. of 1s
for (int row = 1; row < R; row++) {
for (int col = 1; col < C; col++)
if (!isWater(grid[row][col]))
enclaves++;
}
return enclaves;
}
private void checkIfFormsAnIsland(int[][] grid, int row, int col) {
int R = grid.length;
int C = grid[0].length;
int[] dx = new int[] { 0, -1, 0, 1 };
int[] dy = new int[] { -1, 0, 1, 0 };
grid[row][col] = 0;
for (int k = 0; k < dx.length; k++) {
int nrow = row + dx[k];
int ncol = col + dy[k];
if (nrow >= 0 && ncol >= 0 && nrow < R && ncol < C && !isWater(grid[nrow][ncol])) {
checkIfFormsAnIsland(grid, nrow, ncol);
}
}
}
}