Island After Adds Problem

Island After Adds Problem — ExecCode Hard DSA Practice

Solve the Island After Adds problem on ExecCode. Free online hard DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Start with water. Positions are turned into land one by one. After each add, report the number of islands (4-connected land). Duplicate adds on already-land cells should not change the count. This is Number of Islands II, a union-find OA classic. Return an array of island counts after each add. You are given: m — rows n — cols positions — list of [r,c] adds in order Return integer array, same length as positions.

Examples

Input {'m': 3, 'n': 3, 'positions': [[0, 0], [0, 1], [1, 2], [2, 1]]}; Output [1, 1, 2, 3]. Input {'m': 1, 'n': 1, 'positions': [[0, 0], [0, 0]]}; Output [1, 1]. Input {'m': 2, 'n': 2, 'positions': [[0, 0], [1, 1], [0, 1]]}; Output [1, 2, 1]

Constraints

4-neighborhood Adding land that touches existing land merges islands

Practice Island After Adds free on ExecCode. Browse DSA problems, topic map, and placement guides.