Union-Find
Number Theory
·6 mins
Algorithm
Math
Number-Theory
Prime
Sieve
Union-Find
Gcd
GCD, primality tests, the sieve of Eratosthenes / linear sieve / smallest prime factor (SPF), connecting numbers by prime factors with Union-Find, and splitting an array by the last occurrence of prime factors
BFS and Graph Traversal
·6 mins
Algorithm
Bfs
Dfs
Graph
Matrix
Union-Find
Connected components, grid flood fill, multi-source BFS, state-space BFS, shortest cycles, and recursion depth in DFS
LeetCode 827: Making A Large Island
·3 mins
LeetCode
Hard
Dfs
Bfs
Matrix
Union-Find
LeetCode 827 note: label every island and record its size, then for each 0 add the sizes of the distinct neighboring islands plus 1
LeetCode 305: Number of Islands II
·3 mins
LeetCode
Hard
Union-Find
Hash-Table
Array
LeetCode 305 note: each new land cell adds an island, then merges with neighboring land, subtracting 1 per successful merge; skip repeated positions
LeetCode 1998: GCD Sort of an Array
·3 mins
LeetCode
Hard
Math
Number-Theory
Union-Find
Sieve
Sorting
Array
LeetCode 1998 note: numbers in one component can be permuted freely; union each number with its prime factors via SPF, and the array sorts iff each position’s old and new values share a group
LeetCode 1627: Graph Connectivity With Threshold
·2 mins
LeetCode
Hard
Math
Number-Theory
Union-Find
Array
LeetCode 1627 note: no pairwise GCDs; for each z > threshold, connect all multiples of z, O(n log n) unions in total
LeetCode 323: Number of Connected Components in an Undirected Graph
·2 mins
LeetCode
Medium
Graph
Dfs
Bfs
Union-Find
LeetCode 323 note: count components with DFS or Union-Find; Union-Find without path compression is much slower
LeetCode 3873: Maximum Points Activated with One Addition
·2 mins
LeetCode
Hard
Union-Find
Hash-Table
Array
LeetCode 3873 note: merge points sharing an x or y into components; a new point joins at most two of them, so the answer is the two largest plus 1
LeetCode 2421: Number of Good Paths
·3 mins
LeetCode
Hard
Tree
Union-Find
Graph
Sorting
LeetCode 2421 note: add nodes in increasing value order and count same-value nodes per Union-Find group
LeetCode 2382: Maximum Segment Sum After Removals
·2 mins
LeetCode
Hard
Union-Find
Prefix-Sum
Ordered-Set
Array
LeetCode 2382 note: work backward so removals become additions; the Union-Find root stores the segment sum, and the maximum only grows
LeetCode 3910: Count Connected Subgraphs with Even Node Sum
·3 mins
LeetCode
Hard
Bit-Manipulation
Enumeration
Graph
Union-Find
LeetCode 3910 note: n is tiny, so enumerate subsets with a bitmask, check the even sum first, then check that the induced subgraph is connected
LeetCode 1971: Find if Path Exists in Graph
·2 mins
LeetCode
Easy
Graph
Union-Find
LeetCode note for Find if Path Exists in Graph, rebuilt from the original learning note