Islands after each new land cell
A map with rows × cols cells starts as all water. Each [r, c] in cells turns that cell into land, in order. An island is a group of land cells joined up, down, left or right. Return a list with the number of islands after each addition.
The same cell may appear more than once; adding it again changes nothing. Aim for near-constant time per addition instead of recounting the whole map.
Besides the tests, one large input (a 40 × 40 map, 1,600 additions) with a ~200 ms limit (scaled to your device's speed): brute force won't pass.
Examples
islands_after_each(3, 3, [[0,0],[0,1],[1,2],[2,1]])→[1,1,2,3]islands_after_each(2, 3, [[0,0],[0,2],[0,1]])→[1,2,1]islands_after_each(1, 1, [[0,0],[0,0]])→[1,1]