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 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
🏝️ Dynamic Island Formation VisualizationOperation Timeline1Add (0,0)Islands: 0 → 12Add (0,1)Islands: 1 → 13Add (1,2)Islands: 1 → 24Add (2,1)Islands: 2 → 3Union-Find OperationsInitial StateParent:0123Count:0Add (0,0)0123Count:1Union (0,0)-(0,1)0023Count:1Add (1,2)0025Count:2⚡ Performance ComparisonBrute ForceO(k·m·n)Union-FindO(k·α(mn))≈ 100x faster for large grids!
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)

n
2n
Linear Growth
Space Complexity
O(mn)

Union-Find parent array and rank array, plus set to track added positions

n
2n
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
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 25
67.2K Views
High Frequency
~25 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