Skip to content

Practice Minimize Maximum Component Cost

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3613
LeetCode Problem

Minimize Maximum Component Cost

Problem Statement

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 ui and node vi with weight wi, and an integer k. You are allowed to remove any number of edges from the graph such that the resulting graph has at most k connected components. The cost of a component is defined as the maximum edge weight in that component. If a component has no edges, its cost is 0. Return the minimum possible value of the maximum cost among all components after such removals.

Source: Minimize Maximum Component Cost on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 5, edges = [[0,1,4],[1,2,3],[1,3,2],[3,4,6]], k = 2
Output
4

# Example 2

Input
n = 4, edges = [[0,1,5],[1,2,5],[2,3,5]], k = 1
Output
5

Constraints

1 <= n <= 5 * 10^4
0 <= edges.length <= 10^5
edges[i].length == 3
0 <= ui, vi < n
1 <= wi <= 10^6
1 <= k <= n
The input graph is connected.

Related LeetCode Problems

Practice more problems similar to Minimize Maximum Component Cost.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.