Search⌘ K
AI Features

Solution: Number of Islands II

Explore how to use the union-find data structure to solve the Number of Islands II problem by dynamically tracking island counts as land cells are added. Understand the step-by-step solution that merges connected lands and maintain efficient time and space complexity for large inputs.

Statements

You are given a 2D2D binary grid of size m×nm × n. In this grid, 00 represents water and 11 represents land. At the beginning, every cell in the grid contains water (i.e., all cells are 00).

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<=1 <= ...