Union Find
Expert Answer & Key Takeaways
Number of Islands
Examples
Level I: DFS (Matrix Traversal)
Intuition
Detailed Dry Run
Level III: Union-Find (Grid Logic)
Intuition
Detailed Dry Run
Graph Valid Tree
Level I: BFS (Connectivity & Reachability)
Intuition
Detailed Dry Run
Level III: Union-Find (Cycle Detection)
Intuition
Detailed Dry Run
Number of Provinces
isConnected where isConnected[i][j] = 1 if city and city are directly connected. Return the total number of provinces (connected components).Examples
Level I: DFS (Component Counting)
Intuition
Detailed Dry Run
Level III: Union-Find (Component ID)
Intuition
Detailed Dry Run
Accounts Merge
accounts[i] is a list of strings, the first element accounts[i][0] is a name, and the rest are emails. Merge accounts belonging to the same person. Two accounts belong to the same person if there is some common email to both accounts.Examples
Level I: DFS (Graph of Emails)
Intuition
Detailed Dry Run
Level III: Union-Find (Email Mapping)
Intuition
Detailed Dry Run
Redundant Connection
Examples
Level I: DFS (Incremental Cycles)
Intuition
Detailed Dry Run
Level III: Union-Find (Efficiency)
Intuition
Detailed Dry Run
Number of Connected Components
Examples
Level I: DFS (Graph Traversal)
Intuition
Detailed Dry Run
Level III: Union-Find (Direct)
Intuition
Detailed Dry Run
Satisfiability of Equality Equations
Examples
Level III: Union-Find (Double Pass)
Intuition
Detailed Dry Run
Most Stones Removed
Examples
Level I: DFS (Component Counting)
Intuition
Detailed Dry Run
Level III: Union-Find (Row-Column Mapping)
Intuition
r with column c. The number of stones removed is (Total Stones - Number of components).Detailed Dry Run
Smallest String With Swaps
s and pairs of indices that can be swapped, return the lexicographically smallest string possible.Examples
Level III: Union-Find + Sorting (Optimal)
Intuition
Detailed Dry Run
Bricks Falling When Hit
1 represents a brick. A brick will not fall if it is connected to the top row (row index 0) or another brick that will not fall. When a brick is 'hit', it turns into 0. Return the number of bricks that fall after each hit.Reverse Union-Find Strategy
- Remove all hit bricks first.
- Build the initial graph for the remaining bricks.
- Add hit bricks back in reverse order.
- The number of new bricks connected to the top row (excluding the re-added brick itself) is the number of falling bricks for that hit.
Examples
Level II: BFS (Exhaustive Simulation)
Intuition
Level III: Reverse DSU (Connectivity Recovery)
Intuition
Redundant Connection II
- A node has two parents.
- There is a directed cycle.
Strategy
- Check if any node has two parents.
- If yes, test if removing the second edge fixes everything. If not, the first edge was the problem.
- If no node has two parents, simply find the edge that completes a cycle using Union-Find (like Redundant Connection I).
Examples
Level I: DFS (Recursive Search)
Intuition
Level III: Union-Find + Case Analysis
Intuition
Critical Connections in a Network
Relation to DSU
Examples
Level II: Brute-Force DFS (Bridge Detection)
Intuition
Level III: Tarjan's Algorithm (Standard)
Intuition
Minimum Score of a Path Between Two Cities
n representing n cities numbered from 1 to n. You are also given a 2D array roads where roads[i] = [ai, bi, distancei] indicates that there is a bidirectional road between cities ai and bi with a distance distancei. The score of a path between two cities is defined as the minimum distance of a road in this path. Return the minimum possible score of a path between city 1 and city n.Insight
1 and n are connected, you can traverse any road in their connected component multiple times. Thus, the answer is simply the minimum weight road in the entire component containing city 1.Examples
Level I: DFS (Component Traversal)
Intuition
Level III: Union-Find (Component Min Edge)
Intuition
Remove Max Number of Edges to Keep Graph Fully Traversable
- Type 1: Only Alice can traverse.
- Type 2: Only Bob can traverse.
- Type 3: Both can traverse. Return the maximum number of edges you can remove such that both can traverse the entire graph.
Strategy
- Prioritize Type 3 edges: Using one Type 3 edge replaces one Type 1 AND one Type 2 edge.
- Use two DSU instances:
dsuAliceanddsuBob. - After using all potential Type 3 edges, use Type 1 for Alice and Type 2 for Bob.
- If either graph is not connected (components > 1), return -1.
Examples
Level II: BFS (Exhaustive Connectivity)
Intuition
Level III: Two Union-Finds + Greedy Edge Selection
Intuition
Longest Consecutive Sequence
nums, return the length of the longest consecutive elements sequence.Union-Find Perspective
nums contains x and x+1, we can Union(x, x+1). The size of the largest component is the answer.Examples
Level I: Sorting + HashSet
Intuition
Level III: Union-Find (Set Growth)
Intuition
num. If num+1 exists in the set, union num and num+1. Keep track of the size of each component.Couples Holding Hands
Mathematical Insight
Examples
Level II: Greedy Swaps
Intuition
Level III: Union-Find (Cycle Decomposition)
Intuition
Regions Cut By Slashes
Visualization (4 per cell)
/\
/ 0\
<3 1>
\ 2/
\/ - '/' connects 0-3 and 1-2.
- '\' connects 0-1 and 2-3.
- ' ' connects 0-1-2-3.
Examples
Level II: BFS (Grid Expansion)
Intuition
Level III: Union-Find (Internal Grid Mapping)
Intuition
Minimize Malware Spread
Optimization Strategy
Examples
Level II: DFS (Component Analysis)
Intuition
initial are part of it. If only one node is infected, removing it saves all nodes in that component. Track the best node to remove.Level III: Union-Find (Component Size Analysis)
Intuition
initial are in it. If a component has exactly one initial node, moving that node saves size nodes. Pick the one that saves the most.Making a Large Island
0 to 1 in a binary grid to get the largest possible island.Examples
Level III: Union-Find (Component Sizes)
Intuition
Detailed Dry Run
Swim in Rising Water
t needed to reach the bottom-right from the top-left in a grid with elevations.Examples
Level III: Union-Find (Threshold Connectivity)
Intuition
Detailed Dry Run
Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.