Count Sub Islands - Problem
Island Adventure: Finding Sub-Islands!
You're an oceanographer studying two different maps of the same ocean region. You have two
โข grid1: The reference map (0 = water, 1 = land)
โข grid2: The current map (0 = water, 1 = land)
An island is a group of connected land cells (1's) that touch horizontally or vertically. Your mission is to find how many islands in
What makes a sub-island?
An island in
Goal: Return the total count of valid sub-islands in
You're an oceanographer studying two different maps of the same ocean region. You have two
m x n binary grids representing the same area at different times:โข grid1: The reference map (0 = water, 1 = land)
โข grid2: The current map (0 = water, 1 = land)
An island is a group of connected land cells (1's) that touch horizontally or vertically. Your mission is to find how many islands in
grid2 are completely contained within islands from grid1.What makes a sub-island?
An island in
grid2 is a sub-island if every single cell of that island corresponds to land (1) in grid1. If even one cell of the island in grid2 is water (0) in grid1, then it's not a sub-island.Goal: Return the total count of valid sub-islands in
grid2. Input & Output
example_1.py โ Basic Sub-Island Detection
$
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
๐ก Note:
Grid2 has several islands, but only 3 qualify as sub-islands: (1) Top-left island at (0,0)-(0,2) matches land in grid1, (2) Island at (1,3)-(1,4) corresponds to land in grid1, (3) Single cell island at (3,0) matches grid1. Other islands fail because they contain cells that are water in grid1.
example_2.py โ All Valid Sub-Islands
$
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,0,0,0,0],[0,1,1,1,0],[0,0,0,0,0]]
โบ
Output:
1
๐ก Note:
Grid2 has 2 islands: one large island in row 1 and a smaller island in row 3. The large island (1,0)-(1,4) perfectly matches land in grid1, making it a valid sub-island. The smaller island (3,1)-(3,3) also corresponds to land in grid1, making it valid too. Wait - let me recount: there's actually 1 connected island spanning positions that all correspond to land in grid1.
example_3.py โ No Valid Sub-Islands
$
Input:
grid1 = [[1,0,0,0,1],[0,1,1,1,0],[0,1,1,1,0],[0,1,1,1,0],[1,0,0,0,1]]
grid2 = [[0,1,1,1,0],[1,0,0,0,1],[1,0,0,0,1],[1,0,0,0,1],[0,1,1,1,0]]
โบ
Output:
0
๐ก Note:
Every island in grid2 contains at least one cell that corresponds to water (0) in grid1. For example, the top island in grid2 at positions (0,1)-(0,3) overlaps with water in grid1, making it invalid. Similarly, all other islands fail the sub-island test.
Visualization
Tap to expand
Understanding the Visualization
1
Island Discovery
Find connected land cells in grid2 using DFS traversal
2
Simultaneous Validation
For each cell in current island, verify it corresponds to land in grid1
3
Early Termination
If any cell fails validation, mark entire island as invalid
4
Count Valid Islands
Increment counter only for islands that pass complete validation
Key Takeaway
๐ฏ Key Insight: Use DFS to explore each island while simultaneously validating every cell against grid1. Any invalid cell invalidates the entire island - this single-pass approach is both elegant and optimal!
Time & Space Complexity
Time Complexity
O(m*n)
Single pass through grid with each cell visited exactly once during DFS exploration
โ Linear Growth
Space Complexity
O(m*n)
Recursion stack for DFS, worst case when entire grid forms one connected island
โก Linearithmic Space
Constraints
- Grid dimensions: m == grid1.length == grid2.length
- Grid dimensions: n == grid1[i].length == grid2[i].length
- Size limits: 1 โค m, n โค 500
- Cell values: grid1[i][j] and grid2[i][j] are either 0 or 1
- Definition: Islands are 4-directionally connected (no diagonal connections)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code