Solution: Number of Islands II
Understand how to apply the union find algorithm to dynamically calculate the number of islands in a 2D grid as land is added. This lesson guides you through implementing the algorithm with an emphasis on time and space efficiency, enabling you to handle connectivity problems in coding interviews.
We'll cover the following...
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:
...