Island Adventure: Finding Sub-Islands!

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
Sub-Island Detection WorkflowGrid 1 (Template)Grid 2 (Validation Result)DFSExplore + ValidateReal-time Validation ProcessFor each island cell:โœ“ Check if grid1[i][j] == 1โœ“ Continue DFS explorationโœ— Invalid โ†’ Entire island failsFinal Result: 2 Sub-Islandsโœ… Top-left 2x1 island: VALIDโœ… Bottom-right 2x1 island: VALIDโŒ Other islands: INVALIDValid Sub-IslandInvalid Island
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

n
2n
โœ“ Linear Growth
Space Complexity
O(m*n)

Recursion stack for DFS, worst case when entire grid forms one connected island

n
2n
โšก 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)
Asked in
Google 42 Amazon 38 Meta 35 Microsoft 28 Apple 22
48.2K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen