Number of Islands II
Given an m x n grid initially all water (0), you perform a sequence of addLand operations at positions[i] = [r, c], turning that cell into land (1). After each operation, report the current number of islands, where an island is a group of land cells connected 4-directionally. Return the list of counts, one per operation.
Open official problem prompt ↗Maintain the number of connected land components as land cells are added one by one, answering after each addition.
Think of lighting up tiles on a floor one at a time. Each new lit tile is a fresh puddle of light; wherever it touches an already-lit neighbor, two puddles merge into one.
- Input
- m = 3, n = 3, positions = [[0,0],[0,1],[1,2],[2,1]]
- Output
- [1,1,2,3]
- Why
- Add (0,0): 1 island. Add (0,1): merges with (0,0), still 1. Add (1,2): isolated, 2 islands. Add (2,1): isolated, 3 islands.
1 <= m, n, positions.length <= 10^41 <= m * n <= 10^4positions[i].length == 20 <= ri < m0 <= ci < n