You are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land). An island is a group of 1's connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells.
An island in grid2 is considered a sub-island if there is an island in grid1 that contains all the cells that make up this island in grid2.
Return the number of islands in grid2 that are considered sub-islands.
Example 1:
Input: 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]], 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]] Output: 3 Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2. The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
Example 2:
Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]] Output: 2 Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2. The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
Constraints:
m == grid1.length == grid2.lengthn == grid1[i].length == grid2[i].length1 <= m, n <= 500grid1[i][j] and grid2[i][j] are either 0 or 1.class Solution {
private class UnionFind {
int[] root;
int[] rank;
public UnionFind(int size) {
this.root = new int[size];
this.rank = new int[size];
for (int i = 0; i < size; i++) {
root[i] = i;
rank[i] = 1;
}
}
public int find(int x) {
if (root[x] == x)
return x;
return root[x] = find(root[x]);
}
public void union(int x, int y) {
int rootX = root[x];
int rootY = root[y];
if (rootX != rootY) {
if (rank[rootX] > rank[rootY]) {
root[rootY] = rootX;
} else if (rank[rootX] < rank[rootY]) {
root[rootX] = rootY;
} else {
root[rootX] = rootY;
rank[rootY]++;
}
}
}
public boolean connected(int x, int y) {
return find(x) == find(y);
}
}
// Helper to encode node T into unique id for UnionFind DS
private int getId(int i, int j, int n) {
return i * n + j;
}
private static final int[][] directions = {
{ -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 }
};
private boolean isValid(int i, int j, int m, int n) {
return (i >= 0 && i < m && j >= 0 && j < n);
}
private void joinLand(int i, int j, int m, int n, int[][] grid, UnionFind uf) {
for (int[] dir : directions) {
int r = i + dir[0], c = j + dir[1];
if (isValid(r, c, m, n) && grid[r][c] == 1)
uf.union(getId(i, j, n), getId(r, c, n));
}
}
public int countSubIslands(int[][] grid1, int[][] grid2) {
int m = grid1.length, n = grid1[0].length, size = m * n;
UnionFind uf = new UnionFind(size);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid2[i][j] == 1)
joinLand(i, j, m, n, grid2, uf);
}
}
boolean[] isNotSubIsland = new boolean[size];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (grid1[i][j] != 1 && grid2[i][j] == 1)
isNotSubIsland[uf.find(getId(i, j, n))] = true;
int subIslandsCt = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (grid2[i][j] == 1 && !isNotSubIsland[uf.find(getId(i, j, n))]){
subIslandsCt++;
isNotSubIsland[uf.find(getId(i, j, n))] = true;
}
return subIslandsCt;
}
}