Union Find

Union Find

Thirty-five questions on the union-find (disjoint set) data structure, for connectivity and grouping problems.

Find if Path Exists in Graph

easy
There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive).

Accounts Merge

medium
Given a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails...

Check if There is a Valid Path in a Grid

medium
You are given an m x n grid.

Count Islands With Total Value Divisible by K

medium
You are given an m x n matrix grid and a positive integer k.

Count Servers that Communicate

medium
You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server.

Count Sub Islands

medium
You are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land).

Count the Number of Complete Components

medium
You are given an integer n.

Count Unreachable Pairs of Nodes in an Undirected Graph

medium
You are given an integer n.

Detect Cycles in 2D Grid

medium
Given a 2D array of characters grid of size m x n, you need to find if there exists any cycle consisting of the same value in grid.

Evaluate Division

medium
You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i].

Find the Safest Path in a Grid

medium
You are given a 0-indexed 2D matrix grid of size n x n, where (r, c) represents: You are initially positioned at cell (0, 0).

Is Graph Bipartite?

medium
There is an undirected graph with n nodes, where each node is numbered between 0 and n - 1.

Lexicographically Smallest Equivalent String

medium
You are given two strings of the same length s1 and s2 and a string baseStr.

Longest Consecutive Sequence

medium
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

Make Lexicographically Smallest Array by Swapping Elements

medium
You are given a 0-indexed array of positive integers nums and a positive integer limit.

Max Area of Island

medium
You are given an m x n binary matrix grid.

Maximum Number of Fish in a Grid

medium
You are given a 0-indexed 2D matrix grid of size m x n, where (r, c) represents: A fisher can start at any water cell (r, c) and can do the following operations any number of...

Min Cost to Connect All Points

medium
You are given an array points representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi].

Minimize Hamming Distance After Swap Operations

medium
You are given two integer arrays, source and target, both of length n.

Minimize Maximum Component Cost

medium
You are given an undirected connected graph with n nodes labeled from 0 to n - 1 and a 2D integer array edges where edges[i] = [ui, vi, wi] denotes an undirected edge between node...

Minimum Score of a Path Between Two Cities

medium
You are given a positive integer n representing n cities numbered from 1 to n.

Minimum Time for K Connected Components

medium
You are given an integer n and an undirected graph with n nodes labeled from 0 to n - 1.

Most Stones Removed with Same Row or Column

medium
On a 2D plane, we place n stones at some integer coordinate points.

Number of Closed Islands

medium
Given a 2D grid consists of 0s (land) and 1s (water).

Number of Enclaves

medium
You are given an m x n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell.

Number of Operations to Make Network Connected

medium
There are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers...

Number of Provinces

medium
There are n cities.

Path Existence Queries in a Graph I

medium
You are given an integer n representing the number of nodes in a graph, labeled from 0 to n - 1.

Path With Minimum Effort

medium
You are a hiker preparing for an upcoming hike.

Possible Bipartition

medium
We want to split a group of n people (labeled from 1 to n) into two groups of any size.

Power Grid Maintenance

medium
You are given an integer c representing c power stations, each with a unique identifier id from 1 to c (1‑based indexing).

Properties Graph

medium
You are given a 2D integer array properties having dimensions n x m and an integer k.

Reachable Nodes With Restrictions

medium
There is an undirected tree with n nodes labeled from 0 to n - 1 and n - 1 edges.

Redundant Connection

medium
In this problem, a tree is an undirected graph that is connected and has no cycles.

Regions Cut By Slashes

medium
An n x n grid is composed of 1 x 1 squares where each 1 x 1 square consists of a '/', '\', or blank space ' '.