CodeMosa

Master LeetCode Patterns

5 Problems

Union-Find (DSU) Practice Problems

Dynamic connectivity, Kruskal’s MST, and grouping via disjoint-set union with path compression.

Back to Collections

Relevant Patterns

#
Study these pattern guides before tackling the problems. Understanding the core concepts will help you solve them more efficiently.

Practice Problems

#
Show 2 hints
  • Union cities with edges; count unique roots at the end.
  • Alternatively BFS/DFS per component.
Show 2 hints
  • Return first edge forming a cycle while unioning.
  • Union‑by‑rank + path compression for near O(1).
Medium#721
Show 2 hints
  • Union accounts by shared emails; group by root and sort addresses.
  • Map email → id; careful with name grouping.
Show 2 hints
  • Union by row/col keys; answer = n - number of components.
  • Coordinate compression on rows/cols to single DSU space.
Show 2 hints
  • Union index pairs; sort characters within each component and rebuild string.
  • Min‑heap per component to pop smallest letters.

Track Your Progress

Mark problems as complete as you solve them. Come back anytime to continue where you left off.