Statements
You are given a
You can perform an add land operation that changes a water cell into land. An array positions is provided, where each element positions[i] = [ri, ci] indicates the cell (ri, ci), where the ith operation is applied.
Your task is to return an array of integers answer, where answer[i] gives the number of islands present in the grid after executing the ith operation.
An island is defined as a cluster of land cells connected horizontally or vertically (diagonal connections are not considered). The grid is assumed to be completely enclosed by water along its four boundaries.
Constraints:
1<= m,n,positions.length<=104 1<= m * n<=104 positions[i].length==2 0<= ri< m0<= ci< n
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Number of Islands II
Given m = 3, n = 3, and positions = [[0,0], [0,1], [1,2], [2,1]], what is the output?
[1,2,3,4]
[1,1,2,3]
[1,2,2,3]
[1,1,1,1]
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.