Gamified challenge · Union-find
The sea is an m × n grid, all water at first. adds[k] = [r, c] turns cell (r, c) into land at step k (a cell may be named again; it just stays land). An island is a group of land cells joined up, down, left or right.
Write islandsOverTime(m, n, adds) (Python: islands_over_time) that returns a list with the number of islands after each step, so it has one number per entry of adds.
Example: m = 3, n = 3, adds = [[0, 0], [0, 1], [1, 2], [2, 1]] → [1, 1, 2, 3]. Cell (0, 1) joins the first island, and (1, 2) only touches (0, 1) diagonally, so it starts a new one.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google