题库chevron_right分类chevron_right并查集
merge_type

并查集

74 道题目
简单: 1中等: 40困难: 33

并查集 是技术面试里最常出现的能力维度之一。建议先掌握基础题型的边界处理,再逐步过渡到模式识别和复杂度 trade-off。

面试场景

高频考察问题建模、边界条件与口头表达的清晰度。

常见误区

只背模板不解释为什么,容易在追问里失分。

练习策略

每轮练 3-5 题,固定复盘复杂度和可替代解法。

推荐练习顺序

#题目难度
128

最长连续序列

Find the length of the longest consecutive elements sequence in an unsorted array using array scanning and hash lookup.

中等
130

被围绕的区域

Transform the matrix in-place by marking regions surrounded by 'X' as 'X', while keeping border-adjacent 'O's intact.

中等
200

岛屿数量

Count the number of distinct islands in a binary grid using array traversal combined with depth-first search exploration…

中等
399

除法求值

Compute the results of division queries from given equations using graph traversal and depth-first search efficiently.

中等
547

省份数量

Solve Number of Provinces by scanning the adjacency matrix and launching DFS once per unvisited city component.

中等
684

冗余连接

Identify and remove the redundant edge that causes a cycle in a graph starting as a tree.

中等
685

冗余连接 II

Find and remove the redundant connection in a directed graph that was originally a rooted tree.

困难
695

岛屿的最大面积

Find the largest connected land area in a binary grid using array traversal and depth-first search efficiently.

中等
721

账户合并

Merge accounts by connecting emails and returning each user's sorted email list using array scanning and hash lookup eff…

中等
765

情侣牵手

This problem requires arranging couples sitting apart in a row with the minimum number of swaps using graph traversal an…

困难
778

水位上升的泳池中游泳

Solve the problem of swimming through a grid of rising water with a binary search on the valid answer space.

困难
785

判断二分图

Determine whether an undirected graph can be split into two independent sets using DFS, BFS, or Union Find patterns.

中等
803

打砖块

Bricks Falling When Hit challenges your ability to simulate brick falls after sequential erasures using Union Find.

困难
827

最大人工岛

Calculate the largest island size by converting at most one zero in a binary grid using array and DFS techniques efficie…

困难
839

相似字符串组

Determine the number of connected groups of similar strings by swapping at most two letters using array scanning and has…

困难
886

可能的二分法

Determine if a group of n people with mutual dislikes can be split into two non-conflicting groups using graph traversal…

中等
924

尽量减少恶意软件的传播

Identify which single infected node to remove to minimize total malware spread in a connected network graph efficiently.

困难
928

尽量减少恶意软件的传播 II

Minimize Malware Spread II asks to minimize the spread of malware in a network of nodes by removing one infected node.

困难
947

移除最多的同行或同列石头

Maximize the number of stones removed from a 2D plane using graph traversal and DFS.

中等
952

按公因数计算最大组件大小

Find the largest connected component in an array where edges exist between numbers sharing a common factor greater than …

困难
959

由斜杠划分区域

Determine the number of regions in a grid divided by slashes using array scanning and union-find techniques efficiently.

中等
990

等式方程的可满足性

Determine if it's possible to assign values to variables such that all equations are satisfied based on equality and ine…

中等
1020

飞地的数量

This problem involves finding the number of land cells that cannot reach the boundary in a grid using DFS and array mani…

中等
1061

按字典序排列最小的等效字符串

Determine the lexicographically smallest string by modeling character equivalences with union find efficiently.

中等
1202

交换字符串中的元素

Find the lexicographically smallest string by swapping characters in given pairs of indices.

中等
1254

统计封闭岛屿的数目

Count the number of closed islands in a 2D grid using array traversal and depth-first search efficiently.

中等
1267

统计参与通信的服务器

Count Servers that Communicate involves identifying servers in a grid that can communicate based on row or column connec…

中等
1319

连通网络的操作次数

Determine the minimum number of operations required to connect all computers in a network with a limited number of cable…

中等
1361

验证二叉树

Validate Binary Tree Nodes problem requires checking if a set of nodes forms a valid binary tree using graph traversal.

中等
1391

检查网格中是否存在有效路径

Check if there is a valid path from the upper-left to the bottom-right corner of a grid using depth-first search.

中等
1489

找到最小生成树里的关键边和伪关键边

Identify critical and pseudo-critical edges in a weighted graph's minimum spanning tree using Union Find efficiently.

困难
1559

二维网格图中探测环

Detect cycles in a 2D character grid using DFS while carefully tracking parent cells to avoid invalid revisits.

中等
1569

将子数组重新排序得到同一个二叉搜索树的方案数

Determine the number of ways to reorder an array to get the same binary search tree (BST) from its insertion order.

困难
1579

保证图可完全遍历

Maximize the number of edges that can be removed while keeping the graph fully traversable for both Alice and Bob.

困难
1584

连接所有点的最小费用

Min Cost to Connect All Points asks for the minimum cost to connect all points on a 2D plane, using manhattan distances …

中等
1627

带阈值的图连通性

In 'Graph Connectivity With Threshold,' determine if cities are connected based on common divisors exceeding a threshold…

困难
1631

最小体力消耗路径

Find the minimum effort required to travel from the top-left to the bottom-right of a grid, considering height differenc…

中等
1632

矩阵转换后的排名

Compute a unique rank matrix using graph indegree with topological ordering, ensuring each element reflects its relative…

困难
1697

检查边长度限制的路径是否存在

Solve the problem of checking if there exists a path between two nodes under edge length constraints using efficient tec…

困难
1722

执行交换操作后的最小汉明距离

Solve Minimize Hamming Distance After Swap Operations by grouping swappable indices and matching value counts inside eac…

中等
1905

统计子岛屿

Identify and count all islands in grid2 that are fully contained within islands of grid1 using DFS traversal efficiently…

中等
1970

你能穿过矩阵的最后一天

Find the last day to walk from top to bottom in a flooded matrix by using binary search and graph traversal techniques.

困难
1971

寻找图中是否存在路径

Determine if a valid path exists between two vertices in a bi-directional graph using traversal techniques.

简单
1998

数组的最大公因数排序

The GCD Sort problem challenges you to sort an array using a specific gcd-based swap method.

困难
2003

每棵子树内缺失的最小基因值

Determine the smallest missing genetic value in each subtree using binary-tree traversal and precise state tracking effi…

困难
2076

处理含限制条件的好友请求

Determine which friend requests can be accepted without violating direct or indirect restrictions using union-find logic…

困难
2092

找出知晓秘密的所有专家

Find all people who receive a secret through meetings using graph traversal with depth-first search efficiently and corr…

困难
2157

字符串分组

Group words into connected sets using operations on characters with string and bit manipulation techniques.

困难
2316

统计无向图中无法互相到达点对数

Calculate the total number of node pairs in an undirected graph that cannot reach each other using DFS, BFS, or Union Fi…

中等
2334

元素值大于变化阈值的子数组

Find the size of a subarray with all elements greater than threshold divided by length using stack-based state managemen…

困难
2368

受限条件下可到达节点的数目

In the 'Reachable Nodes With Restrictions' problem, find the maximum reachable nodes from node 0 in a tree while avoidin…

中等
2382

删除操作后的最大子段和

Calculate the maximum segment sum after sequential removals using array plus union find for efficient merging of segment…

困难
2421

好路径的数目

Count all good paths in a tree by scanning node values and using hash maps to track valid connections efficiently.

困难
2424

最长上传前缀

Calculate the longest continuous uploaded prefix in a video stream efficiently using a mix of binary search and data str…

中等
2492

两个城市间路径的最小分数

Find the minimum distance in a path connecting two cities using graph traversal strategies efficiently.

中等
2493

将节点分成尽可能多的组

Determine the maximum number of groups nodes can form in a graph using depth-first traversal without violating edge conn…

困难
2503

矩阵查询可获得的最大分数

Solve the Maximum Number of Points From Grid Queries problem using two-pointer scanning and invariant tracking.

困难
2573

找出对应 LCP 矩阵的字符串

Determine the lexicographically smallest string matching a given LCP matrix using state transition dynamic programming.

困难
2617

网格图中最少访问的格子数

Determine the minimum number of cells to visit in a grid using state transition dynamic programming and efficient traver…

困难
2658

网格图中鱼的最大数目

Maximize the number of fish that can be caught by performing DFS on an optimal starting point in a grid.

中等
2685

统计完全连通分量的数量

Determine the number of complete connected components in an undirected graph using efficient traversal techniques and gr…

中等
2709

最大公约数遍历

Determine if every index in an array can be reached from any other using traversals based on greatest common divisors.

困难
2812

找出最安全路径

Find the Safest Path in a Grid uses binary search and BFS to maximize path safeness from thieves in a grid.

中等
2948

交换得到字典序最小的数组

Solve the problem of making an array lexicographically smallest through element swaps under a limit constraint.

中等
3108

带权图里旅途的最小代价

Find the minimum cost walk in a weighted graph using array and bit manipulation techniques for efficient path calculatio…

困难
3235

判断矩形的两个角落是否可达

Determine if there is a valid path from the bottom-left to top-right of a rectangle while avoiding circles.

困难
3378

统计最小公倍数图中的连通块数目

Determine the number of connected components in an LCM-based graph using array scanning and hash-based connectivity chec…

困难
3493

属性图

Find the number of connected components in an undirected graph formed by properties arrays, using array scanning and has…

中等
3532

针对图的路径存在性查询 I

Determine if paths exist between nodes using array scanning and hash lookups for efficient component checks in graphs.

中等
3600

升级后最大生成树稳定性

Maximizing the stability of a spanning tree with upgrades requires careful optimization of edge strengths using binary s…

困难
3607

电网维护

Determine which power stations remain connected after maintenance using array scanning and hash lookups to track compone…

中等
3608

包含 K 个连通分量需要的最小时间

Find the minimum time to remove edges such that a graph with n nodes has at least k connected components.

中等
3613

最小化连通分量的最大成本

Minimize Maximum Component Cost leverages binary search over edge weights to optimize the cost of graph components after…

中等
3619

总价值可以被 K 整除的岛屿数目

Count the number of islands in a grid where the sum of each island's values is divisible by a given integer k.

中等

关联高频模式

LeetCode 并查集题型题解:74题训练路线