Number of Islands II

Problem Description

You are given an empty 2D grid with `m` rows and `n` columns, initially filled with water (0). We may perform a sequence of positions to add land (set grid[r][c] = 1). After each addition, return the number of islands. An island is a group of adjacent land cells connected horizontally or vertically. **Example:** Input: ``` m = 3, n = 3, positions = [[0,0],[0,1],[1,2],[2,1]] ``` Output: `[1,1,2,3]` **Explanation:** After adding land at (0,0) -> 1 island (0,1) -> cells (0,0) and (0,1) connect -> 1 island (1,2) -> new isolated cell -> 2 islands (2,1) -> new isolated cell -> 3 islands

Example Test Cases

Example 1
Input: [3,3,[[0,0],[0,1],[1,2],[2,1]]]
Expected: [1,1,2,3]
Example 2
Input: [1,1,[[0,0]]]
Expected: [1]

Run your code to see the results