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
easyThere is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive).
Accounts Merge
mediumGiven 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
mediumYou are given an m x n grid.
Count Islands With Total Value Divisible by K
mediumYou are given an m x n matrix grid and a positive integer k.
Count Servers that Communicate
mediumYou 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
mediumYou 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
mediumYou are given an integer n.
Count Unreachable Pairs of Nodes in an Undirected Graph
mediumYou are given an integer n.
Detect Cycles in 2D Grid
mediumGiven 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
mediumYou 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
mediumYou 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?
mediumThere is an undirected graph with n nodes, where each node is numbered between 0 and n - 1.
Lexicographically Smallest Equivalent String
mediumYou are given two strings of the same length s1 and s2 and a string baseStr.
Longest Consecutive Sequence
mediumGiven an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
Make Lexicographically Smallest Array by Swapping Elements
mediumYou are given a 0-indexed array of positive integers nums and a positive integer limit.
Max Area of Island
mediumYou are given an m x n binary matrix grid.
Maximum Number of Fish in a Grid
mediumYou 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
mediumYou 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
mediumYou are given two integer arrays, source and target, both of length n.
Minimize Maximum Component Cost
mediumYou 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
mediumYou are given a positive integer n representing n cities numbered from 1 to n.
Minimum Time for K Connected Components
mediumYou 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
mediumOn a 2D plane, we place n stones at some integer coordinate points.
Number of Closed Islands
mediumGiven a 2D grid consists of 0s (land) and 1s (water).
Number of Enclaves
mediumYou 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
mediumThere 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
mediumThere are n cities.
Path Existence Queries in a Graph I
mediumYou are given an integer n representing the number of nodes in a graph, labeled from 0 to n - 1.
Path With Minimum Effort
mediumYou are a hiker preparing for an upcoming hike.
Possible Bipartition
mediumWe want to split a group of n people (labeled from 1 to n) into two groups of any size.
Power Grid Maintenance
mediumYou are given an integer c representing c power stations, each with a unique identifier id from 1 to c (1‑based indexing).
Properties Graph
mediumYou are given a 2D integer array properties having dimensions n x m and an integer k.
Reachable Nodes With Restrictions
mediumThere is an undirected tree with n nodes labeled from 0 to n - 1 and n - 1 edges.
Redundant Connection
mediumIn this problem, a tree is an undirected graph that is connected and has no cycles.
Regions Cut By Slashes
mediumAn n x n grid is composed of 1 x 1 squares where each 1 x 1 square consists of a '/', '\', or blank space ' '.