LeetCode 题解工作台

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

给你一个无向连通图,包含 n 个节点,节点编号从 0 到 n - 1 ,以及一个二维整数数组 edges ,其中 edges[i] = [u i , v i , w i ] 表示一条连接节点 u i 和节点 v i 的无向边,边权为 w i ,另有一个整数 k 。 你可以从图中移除任意数量的边,使得…

category

4

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 二分·搜索·答案·空间

bolt

答案摘要

如果 $k = n$,说明所有的边都可以被移除,此时所有的连通分量都是孤立的节点,最大成本为 0。 否则,我们可以将所有的边按权值从小到大排序,然后使用并查集来维护连通分量。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 二分·搜索·答案·空间 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个无向连通图,包含 n 个节点,节点编号从 0 到 n - 1,以及一个二维整数数组 edges,其中 edges[i] = [ui, vi, wi] 表示一条连接节点 ui 和节点 vi 的无向边,边权为 wi,另有一个整数 k

你可以从图中移除任意数量的边,使得最终的图中 最多 只包含 k 个连通分量。

连通分量的 成本 定义为该分量中边权的 最大值 。如果一个连通分量没有边,则其代价为 0。

请返回在移除这些边之后,在所有连通分量之中的 最大成本 的 最小可能值 

 

示例 1:

输入: n = 5, edges = [[0,1,4],[1,2,3],[1,3,2],[3,4,6]], k = 2

输出: 4

解释:

  • 移除节点 3 和节点 4 之间的边(权值为 6)。
  • 最终的连通分量成本分别为 0 和 4,因此最大代价为 4。

示例 2:

输入: n = 4, edges = [[0,1,5],[1,2,5],[2,3,5]], k = 1

输出: 5

解释:

  • 无法移除任何边,因为只允许一个连通分量(k = 1),图必须保持完全连通。
  • 该连通分量的成本等于其最大边权,即 5。

 

提示:

  • 1 <= n <= 5 * 104
  • 0 <= edges.length <= 105
  • edges[i].length == 3
  • 0 <= ui, vi < n
  • 1 <= wi <= 106
  • 1 <= k <= n
  • 输入图是连通图。
lightbulb

解题思路

方法一:排序 + 并查集

如果 k=nk = n,说明所有的边都可以被移除,此时所有的连通分量都是孤立的节点,最大成本为 0。

否则,我们可以将所有的边按权值从小到大排序,然后使用并查集来维护连通分量。

我们不妨假设初始时所有节点并不联通,初始时每个节点都是一个独立的连通分量。我们从权值最小的边开始,尝试将其加入到当前的连通分量中。如果加入后连通分量的数量已经小于等于 kk,则说明剩余的边都可以被移除,此时当前边的权值就是我们要找的最大成本,返回该权值即可。否则,我们继续处理下一条边。

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(n)O(n)。其中 nn 是节点的数量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
    def minCost(self, n: int, edges: List[List[int]], k: int) -> int:
        def find(x: int) -> int:
            if p[x] != x:
                p[x] = find(p[x])
            return p[x]

        if k == n:
            return 0
        edges.sort(key=lambda x: x[2])
        cnt = n
        p = list(range(n))
        for u, v, w in edges:
            pu, pv = find(u), find(v)
            if pu != pv:
                p[pu] = pv
                cnt -= 1
                if cnt <= k:
                    return w
        return 0
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate efficiently handles the graph's connectivity with Union-Find.

  • question_mark

    The candidate demonstrates understanding of binary search applied to optimization problems.

  • question_mark

    The candidate uses edge sorting as a natural step to streamline the solution process.

warning

常见陷阱

外企场景
  • error

    Overlooking the need to sort the edges before applying binary search.

  • error

    Misunderstanding the edge weights when performing Union-Find operations.

  • error

    Failing to handle the k-component constraint correctly, leading to an incorrect solution.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Increase the number of components allowed (k) and observe how it affects the optimization process.

  • arrow_right_alt

    Change the graph from being connected to having multiple disjoint subgraphs and analyze the approach.

  • arrow_right_alt

    Allow negative edge weights and evaluate how the algorithm adapts to this new scenario.

help

常见问题

外企场景

最小化连通分量的最大成本题解:二分·搜索·答案·空间 | LeetCode #3613 中等