Problem
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.
Examples
Example 1:
$$ \begin{bmatrix} \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 \\ \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{orange} 1 \end{bmatrix} \begin{bmatrix} \colorbox{red} 1 & \colorbox{red} 1 & \colorbox{red} 1 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{red} 1 & \colorbox{red} 1 & \colorbox{red} 1 \\ \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{red} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{blue} 0 \\ \colorbox{blue} 0 & \colorbox{red} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 \end{bmatrix} $$
| |
Example 2:
$$ \begin{bmatrix} \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 \\ \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 \\ \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 \\ \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 \end{bmatrix} \begin{bmatrix} \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 \\ \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 \\ \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 \\ \colorbox{red} 1 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{red} 1 \end{bmatrix} $$
| |
Solution
This problem is similar to Number of Islands.
Method 1 - DFS
Return true, if that is a sub-island. Also, when we visit some sub-island, make it visited by adding -1 in the adjacent pieces.
Here are the steps:
- DFS traversal for islands: Use DFS to identify and traverse islands in
grid2. - Check for Sub-island: While traversing an island in
grid2, ensure every land cell (1) of this island corresponds to a land cell ingrid1. - Count Valid Sub-islands: Count islands in
grid2that pass the sub-island check.
Video Explanation
Here is the video explanation:
Code
| |
| |
Complexity
- ⏰ Time complexity:
O(m*n), where m is number of rows and n is number of columns.- Because we go through each cell only once, as when we start the dfs we sink the island to mark it as visited.
- 🧺 Space complexity:
O(m*n)assuming recursion stack