-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode_1905.java
97 lines (85 loc) · 3.36 KB
/
Leetcode_1905.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class Solution {
// Directions in which we can traverse inside the grids.
private final int[][] directions = {
{ 0, 1 },
{ 1, 0 },
{ 0, -1 },
{ -1, 0 },
};
// Helper method to check if the cell at the position (x, y) in the 'grid'
// is a land cell.
private boolean isCellLand(int x, int y, int[][] grid) {
return grid[x][y] == 1;
}
// Traverse all cells of island starting at position (x, y) in 'grid2',
// and check this island is a sub-island in 'grid1'.
private boolean isSubIsland(
int x,
int y,
int[][] grid1,
int[][] grid2,
boolean[][] visited
) {
int totalRows = grid2.length;
int totalCols = grid2[0].length;
boolean isSubIsland = true;
Queue<int[]> pendingCells = new LinkedList<>();
// Push the starting cell in the queue and mark it as visited.
pendingCells.offer(new int[] { x, y });
visited[x][y] = true;
// Traverse on all cells using the breadth-first search method.
while (!pendingCells.isEmpty()) {
int[] curr = pendingCells.poll();
int currX = curr[0];
int currY = curr[1];
// If the current position cell is not a land cell in 'grid1',
// then the current island can't be a sub-island.
if (!isCellLand(currX, currY, grid1)) {
isSubIsland = false;
}
for (int[] direction : directions) {
int nextX = currX + direction[0];
int nextY = currY + direction[1];
// If the next cell is inside 'grid2', is never visited and
// is a land cell, then we traverse to the next cell.
if (
nextX >= 0 &&
nextY >= 0 &&
nextX < totalRows &&
nextY < totalCols &&
!visited[nextX][nextY] &&
isCellLand(nextX, nextY, grid2)
) {
// Push the next cell in the queue and mark it as visited.
pendingCells.offer(new int[] { nextX, nextY });
visited[nextX][nextY] = true;
}
}
}
return isSubIsland;
}
public int countSubIslands(int[][] grid1, int[][] grid2) {
int totalRows = grid2.length;
int totalCols = grid2[0].length;
boolean[][] visited = new boolean[totalRows][totalCols];
int subIslandCounts = 0;
// Iterate on each cell in 'grid2'
for (int x = 0; x < totalRows; ++x) {
for (int y = 0; y < totalCols; ++y) {
// If cell at the position (x, y) in the 'grid2' is not visited,
// is a land cell in 'grid2', and the island
// starting from this cell is a sub-island in 'grid1', then we
// increment the count of sub-islands.
if (
!visited[x][y] &&
isCellLand(x, y, grid2) &&
isSubIsland(x, y, grid1, grid2, visited)
) {
subIslandCounts += 1;
}
}
}
// Return total count of sub-islands.
return subIslandCounts;
}
}