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.length
n == grid1[i].length == grid2[i].length
1 <= m, n <= 500
grid1[i][j]
and grid2[i][j]
are either 0
or 1
.Given two binary matrices grid1
and grid2
of size m x n, where 1 represents land and 0 represents water, find the number of sub-islands in grid2
. A sub-island in grid2
is an island whose cells are all also part of an island in grid1
.
This problem can be efficiently solved using Depth-First Search (DFS). The core idea is to iterate through grid2
. Whenever we encounter a land cell (value 1), we initiate a DFS to explore the entire connected island. During this DFS, we simultaneously check if each cell in the island is also land in grid1
. If even one cell in the grid2
island is water in grid1
, the entire grid2
island is not a sub-island.
grid2
row by row and column by column.grid2[i][j]
is 1 (land), start a DFS exploration from that cell.grid2
by setting it to 0.grid1
is also 1. If not, the island is not a sub-island, so we set isSubIsland
to false.isSubIsland
is still true, increment the count of sub-islands.def countSubIslands(grid1, grid2):
m, n = len(grid1), len(grid1[0])
count = 0
def dfs(i, j):
if not (0 <= i < m and 0 <= j < n and grid2[i][j] == 1):
return True # Base case: out of bounds or water
grid2[i][j] = 0 # Mark as visited
is_sub_island = grid1[i][j] == 1 # Check if this cell is land in grid1
is_sub_island &= dfs(i + 1, j) # Explore down
is_sub_island &= dfs(i - 1, j) # Explore up
is_sub_island &= dfs(i, j + 1) # Explore right
is_sub_island &= dfs(i, j - 1) # Explore left
return is_sub_island
for i in range(m):
for j in range(n):
if grid2[i][j] == 1:
if dfs(i, j):
count += 1
return count
The code in other languages (Java, C++, Go, TypeScript, JavaScript) follows a very similar structure, using the same DFS logic. The primary difference would be in syntax and data structure handling specific to each language. The core algorithm remains the same.