Sponsored
Sponsored
This approach uses Depth-First Search (DFS) to explore each island in grid2, checking if it matches an area in grid1. We iterate through each cell in grid2, initiating a DFS whenever we encounter land (1). During DFS, we verify if the entire island in grid2 can be found in grid1. If during exploration, we encounter a cell that is 1 in grid2 but 0 in grid1, we mark it as not a sub-island.
Time Complexity: O(m * n), where m and n are the dimensions of the grids, as each cell is visited a constant number of times.
Space Complexity: O(1) additional space not counting input and stack space for recursion.
1function dfs(grid1, grid2, i, j, isSubIsland) {
2 if (i < 0 || i >= grid2.length || j < 0 || j >= grid2[0].length || grid2[i][j] === 0) {
3 return;
4 }
5 if (grid1[i][j] === 0) {
6 isSubIsland[0] = false;
7 }
8 grid2[i][j] = 0;
9 dfs(grid1, grid2, i + 1, j, isSubIsland);
10 dfs(grid1, grid2, i - 1, j, isSubIsland);
11 dfs(grid1, grid2, i, j + 1, isSubIsland);
12 dfs(grid1, grid2, i, j - 1, isSubIsland);
13}
14
15function countSubIslands(grid1, grid2) {
16 let count = 0;
17 for (let i = 0; i < grid2.length; i++) {
18 for (let j = 0; j < grid2[0].length; j++) {
19 if (grid2[i][j] === 1) {
20 let isSubIsland = [true];
21 dfs(grid1, grid2, i, j, isSubIsland);
22 if (isSubIsland[0]) {
23 count++;
24 }
25 }
26 }
27 }
28 return count;
29}
30
31// Example usage
32const grid1 = [
33 [1,1,1,0,0],
34 [0,1,1,1,1],
35 [0,0,0,0,0],
36 [1,0,0,0,0],
37 [1,1,0,1,1]
38];
39const grid2 = [
40 [1,1,1,0,0],
41 [0,0,1,1,1],
42 [0,1,0,0,0],
43 [1,0,1,1,0],
44 [0,1,0,1,0]
45];
46console.log(countSubIslands(grid1, grid2));
The JavaScript implementation executes a DFS to discover sub-islands by exploring all potential islands in grid2, ensuring boundary conditions are honored and checking against grid1.
This approach uses the Union-Find data structure to efficiently group connected islands. By initially identifying all islands in both grids, we can ascertain the sub-islands by ensuring that connected components (islands) in grid2 are completely represented within connected components in grid1.
Time Complexity: O(m * n * (α(m*n))), where α is the Inverse Ackermann function, a very slow-growing function.
Space Complexity: O(m * n) to store the parent and rank arrays.
1
This Java implementation deploys the Union-Find class to efficiently group land cells into islands for both grids, then checks if islands in grid2 are completely captured within those in grid1, allowing identification of sub-islands.