Union Find LeetCode Problems: All 83,
With Python Solutions

Every problem in this library that LeetCode tags Union Find. 66 of them carry a complete Python solution, a worked example and the time and space complexity of the approach.

Easy
1
Medium
44
Hard
38
1 pattern

How Union Find problems are solved

A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Union Find problems — each one explains the approach, gives a Python template and states its complexity.

Eyes only

When the Union Find problem arrives live.

In the live round, Stealth Interview reads the problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer says. It's a desktop app for macOS and Windows, invisible to screen sharing.

Union Find problems by difficulty

Problems with a complete Python solution are listed first, then by ascending problem number.

Easy (1)

Easy problems
No.Problem
1971Find if Path Exists in Graph

Medium (44)

Medium problems
No.Problem
0128Longest Consecutive Sequence
0130Surrounded Regions
0200Number of Islands
0399Evaluate Division
0547Number of Provinces
0684Redundant Connection
0695Max Area of Island
0721Accounts Merge
0785Is Graph Bipartite?
0886Possible Bipartition
0947Most Stones Removed with Same Row or Column
0959Regions Cut By Slashes
0990Satisfiability of Equality Equations
1020Number of Enclaves
1061Lexicographically Smallest Equivalent String
1202Smallest String With Swaps
1254Number of Closed Islands
1267Count Servers that Communicate
1319Number of Operations to Make Network Connected
1361Validate Binary Tree Nodes
1391Check if There is a Valid Path in a Grid
1559Detect Cycles in 2D Grid
1584Min Cost to Connect All Points
1631Path With Minimum Effort
1722Minimize Hamming Distance After Swap Operations
1905Count Sub Islands
2316Count Unreachable Pairs of Nodes in an Undirected Graph
2368Reachable Nodes With Restrictions
2424Longest Uploaded Prefix
2492Minimum Score of a Path Between Two Cities
2658Maximum Number of Fish in a Grid
2685Count the Number of Complete Components
2812Find the Safest Path in a Grid
2948Make Lexicographically Smallest Array by Swapping Elements
0261Graph Valid TreePremium
0323Number of Connected Components in an Undirected GraphPremium
0694Number of Distinct IslandsPremium
0737Sentence Similarity IIPremium
1101The Earliest Moment When Everyone Become FriendsPremium
1102Path With Maximum Minimum ValuePremium
1135Connecting Cities With Minimum CostPremium
1258Synonymous SentencesPremium
2782Number of Unique CategoriesPremium
2852Sum of Remoteness of All CellsPremium

Hard (38)

Hard problems
No.Problem
0352Data Stream as Disjoint Intervals
0685Redundant Connection II
0765Couples Holding Hands
0778Swim in Rising Water
0803Bricks Falling When Hit
0827Making A Large Island
0839Similar String Groups
0924Minimize Malware Spread
0928Minimize Malware Spread II
0952Largest Component Size by Common Factor
1489Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree
1569Number of Ways to Reorder Array to Get Same BST
1579Remove Max Number of Edges to Keep Graph Fully Traversable
1627Graph Connectivity With Threshold
1632Rank Transform of a Matrix
1697Checking Existence of Edge Length Limited Paths
1970Last Day Where You Can Still Cross
1998GCD Sort of an Array
2003Smallest Missing Genetic Value in Each Subtree
2076Process Restricted Friend Requests
2092Find All People With Secret
2157Groups of Strings
2334Subarray With Elements Greater Than Varying Threshold
2382Maximum Segment Sum After Removals
2421Number of Good Paths
2493Divide Nodes Into the Maximum Number of Groups
2503Maximum Number of Points From Grid Queries
2573Find the String with LCP
2612Minimum Reverse Operations
2617Minimum Number of Visited Cells in a Grid
2709Greatest Common Divisor Traversal
0305Number of Islands IIPremium
0711Number of Distinct Islands IIPremium
1168Optimize Water Distribution in a VillagePremium
1724Checking Existence of Edge Length Limited Paths IIPremium
2204Distance to a Cycle in Undirected GraphPremium
2307Check for Contradictions in EquationsPremium
2371Minimize Maximum Value in a GridPremium