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.
1#include <vector>
2using namespace std;
3
4void dfs(vector<vector<int>>& grid1, vector<vector<int>>& grid2, int i, int j, bool& isSubIsland) {
5 if (i < 0 || i >= grid2.size() || j < 0 || j >= grid2[0].size() || grid2[i][j] == 0) {
6 return;
7 }
8 if (grid1[i][j] == 0) {
9 isSubIsland = false;
10 }
11 grid2[i][j] = 0;
12 dfs(grid1, grid2, i+1, j, isSubIsland);
13 dfs(grid1, grid2, i-1, j, isSubIsland);
14 dfs(grid1, grid2, i, j+1, isSubIsland);
15 dfs(grid1, grid2, i, j-1, isSubIsland);
16}
17
18int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
19 int m = grid1.size(), n = grid1[0].size(), count = 0;
20 for (int i = 0; i < m; i++) {
21 for (int j = 0; j < n; j++) {
22 if (grid2[i][j] == 1) {
23 bool isSubIsland = true;
24 dfs(grid1, grid2, i, j, isSubIsland);
25 if (isSubIsland) count++;
26 }
27 }
28 }
29 return count;
30}
31
32int main() {
33 vector<vector<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}};
34 vector<vector<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}};
35 int result = countSubIslands(grid1, grid2);
36 cout << result << endl;
37 return 0;
38}
The solution finds islands in grid2 and uses DFS to explore them while checking if they are contained within an island in grid1. If at any point a part of an island in grid2 is found to be not part of an island in grid1, the island in grid2 is disqualified as a sub-island.
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
For JavaScript, this solution uses Union-Find to connect and group islands within grid1, examining grid2 islands for complete overlap to ascertain sub-island status.