Graphs
TimeO(rows·cols)SpaceO(rows·cols)
Scan a grid of land and water. Each time you hit unvisited land, flood-fill the whole connected island so it's counted once — the number of flood-fills is the island count.
Key idea. Flood-fill each unvisited land cell once. Counting connected components = counting how many flood-fills you start.
1
Loading…
—