Number of Islands II — Union-Find Online Queries
Count islands after each addLand operation. Online version requires Union-Find: add each land cell and union with adjacent land cells.
webcoderspeed.com
9 articles
Count islands after each addLand operation. Online version requires Union-Find: add each land cell and union with adjacent land cells.
Count connected components in an undirected graph given as adjacency matrix. DFS or Union-Find both work.
Check if a path exists between source and destination in an undirected graph. BFS from source or Union-Find both work in O(V+E).
Check if n nodes and edges form a valid tree: must be connected and acyclic. Exactly n-1 edges, all connected via BFS/DFS or Union-Find.
Count connected components in an undirected graph. Union-Find or BFS both give O(V+E) solution.
Find the edge that creates a cycle in an undirected graph that started as a tree. Process edges with Union-Find; the first edge whose endpoints share a root is redundant.
Find the minimum spanning tree of a weighted graph using Kruskal's algorithm. Sort edges by weight, use Union-Find to avoid cycles. O(E log E) time.
Count islands after each addLand operation using incremental Union-Find. Each new land cell potentially merges with up to 4 neighbors — classic Amazon system-at-scale problem.
Merge accounts sharing common email addresses using Union-Find. Group emails by owner, merge accounts with shared emails, sort each merged group.