← 返回 google 的题目列表Number of Islands with Incremental Land Additions
类型:online_judge
Number of Islands with Incremental Land Additions
Given an m x n grid of '0' (water) and '1' (land), horizontally or vertically adjacent land cells belong to the same island.
Implement numIslands(grid) to return the number of islands.
Follow-up: the grid initially contains only water. Given a sequence of land positions to add, return the island count after each addition.
If a position is added more than once, the island count remains unchanged for that operation.
Example
m = 3, n = 3
positions = [[0,0], [0,1], [1,2], [2,1]]
Output: [1, 1, 2, 3]
Constraints
1 <= m, n <= 10^4
Number of additions k <= 10^5
Do not rescan the whole grid after every addition.
Example
Input
3 3
4
0 0
0 1
1 2
2 1
Output
1 1 2 3