Number of Islands II - Problem
Number of Islands II is a dynamic programming problem where you simulate the formation of islands over time.
You start with an empty
An island is a group of connected land cells (horizontally or vertically adjacent). When you add land, it might:
• Create a new island
• Join existing islands together
• Simply extend an existing island
Your task is to return an array where each element represents the number of islands after performing each land addition operation.
Example: If you have a 3×3 grid and add land at positions [[0,0], [0,1], [1,2], [2,1]], the island counts would change as you connect or create new land masses.
You start with an empty
m × n grid filled with water (represented by 0's). You're given a sequence of operations where each operation adds land (represented by 1) at a specific position. After each land addition, you need to count how many islands exist.An island is a group of connected land cells (horizontally or vertically adjacent). When you add land, it might:
• Create a new island
• Join existing islands together
• Simply extend an existing island
Your task is to return an array where each element represents the number of islands after performing each land addition operation.
Example: If you have a 3×3 grid and add land at positions [[0,0], [0,1], [1,2], [2,1]], the island counts would change as you connect or create new land masses.
Input & Output
example_1.py — Basic Island Formation
$
Input:
m = 3, n = 3, positions = [[0,0],[0,1],[1,2],[2,1]]
›
Output:
[1,1,2,3]
💡 Note:
Initially grid is empty. Add (0,0) → 1 island. Add (0,1) → joins with (0,0), still 1 island. Add (1,2) → new island, total 2. Add (2,1) → new island, total 3.
example_2.py — Island Merging
$
Input:
m = 1, n = 1, positions = [[0,0]]
›
Output:
[1]
💡 Note:
Single cell grid. Adding land at (0,0) creates exactly 1 island.
example_3.py — Duplicate Positions
$
Input:
m = 3, n = 3, positions = [[0,0],[0,1],[0,0],[1,1]]
›
Output:
[1,1,1,2]
💡 Note:
Add (0,0) → 1 island. Add (0,1) → merge, still 1. Try (0,0) again → no change, still 1. Add (1,1) → connects to existing island, still 1... Wait, this would actually create 2 islands if (1,1) doesn't connect diagonally.
Visualization
Tap to expand
Understanding the Visualization
1
Initialize
Start with empty ocean (Union-Find with m×n cells)
2
Add Land
Place land and assume it creates a new island (+1 count)
3
Check Neighbors
Look at 4 adjacent cells for existing land
4
Union Components
For each land neighbor, merge components and decrement count
5
Record State
Add current island count to result array
Key Takeaway
🎯 Key Insight: Union-Find transforms an O(k·m·n) problem into O(k·α(mn)) by maintaining dynamic connectivity without expensive grid traversals. Each land addition requires only checking 4 neighbors and potentially 4 union operations.
Time & Space Complexity
Time Complexity
O(k × α(mn))
k operations, each with at most 4 union operations. α is inverse Ackermann function (practically constant)
✓ Linear Growth
Space Complexity
O(mn)
Union-Find parent array and rank array, plus set to track added positions
⚡ Linearithmic Space
Constraints
- 1 ≤ m, n ≤ 104
- 1 ≤ positions.length ≤ 104
- 0 ≤ positions[i][0] < m
- 0 ≤ positions[i][1] < n
- positions may contain duplicate coordinates
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code