I knew this was a union-find problem pretty fast, which was a relief.
Use Union-Find (Disjoint Set Union) to dynamically track connected components as land cells are added. For each new land cell, increment the island count by 1, then check its four neighbors; if a neighbor is land, union the two cells and decrement the count for each successful union. This yields O(α(mn)) time per addition, which is optimal.
Pro tip: Mention that you can optimize by only checking neighbors that have already been added (e.g., up and left) to avoid redundant checks, and emphasize that Union-Find with path compression and union by rank is the standard efficient solution for dynamic connectivity problems like this.
Confirm the grid dimensions, the order of additions, and that islands are 4-directionally connected. Ask about edge cases like duplicate positions or out-of-bounds coordinates.
Select Union-Find (Disjoint Set Union) to efficiently manage connected components. Explain that it supports near-constant time union and find operations with path compression and union by rank.
Initialize a parent array for all cells, a rank array, and a counter for islands. For each position, if already land, skip; else mark as land, increment island count, and for each valid neighbor that is land, union the cells and decrement count on successful union.
State that each addition takes O(α(mn)) amortized time, where α is the inverse Ackermann function, and space is O(mn). Discuss handling duplicate positions, boundary checks, and the initial state (zero islands).
Walk through a small example (e.g., 3x3 grid with positions [[0,0],[0,1],[1,2],[2,1]]) to verify the island count updates correctly after each addition.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.