Watch 6 video solutions for Minimize Maximum Component Cost, a medium level problem involving Binary Search, Union Find, Graph. This walkthrough by Samrat Bhardwaj has 573 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: n = 5, edges = [[0,1,4],[1,2,3],[1,3,2],[3,4,6]], k = 2
Output: 4
Explanation:

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

k = 1) requires the graph to stay fully connected.
Constraints:
1 <= n <= 5 * 1040 <= edges.length <= 105edges[i].length == 30 <= ui, vi < n1 <= wi <= 1061 <= k <= nProblem Overview: You are given a graph where edges or nodes contribute a cost to the connected component they belong to. The goal is to combine vertices while keeping the largest component cost as small as possible. The challenge is choosing the order of connections so the maximum cost among all resulting components stays minimized.
Approach 1: Brute Force Threshold Simulation (O(E * (V + E)) time, O(V) space)
A direct approach tries every possible cost threshold. For each candidate maximum cost, iterate through the edges and build components only if the resulting component does not exceed that threshold. This requires repeatedly traversing edges and rebuilding connectivity using DFS/BFS or a temporary disjoint structure. While it helps reason about the constraint ("can we keep every component ≤ X?"), it becomes expensive because the graph is rebuilt for each candidate threshold.
Approach 2: Sorting + Union-Find (O(E log E) time, O(V) space)
The efficient solution processes edges in increasing cost order, similar to Kruskal’s algorithm. First sort edges by cost. Then iterate through them and merge components using a Union-Find (Disjoint Set Union) structure. Each union operation combines two components and updates the cost contribution for that merged component. Because edges are processed from smallest to largest, the moment a component forms, its maximum contributing cost is already minimized.
The Union-Find structure tracks component parents and optionally component metadata such as accumulated cost or size. With path compression and union by rank, each operation runs in near-constant time O(α(V)). Sorting dominates the complexity, resulting in O(E log E) overall time. This greedy strategy works because delaying expensive edges ensures the maximum component cost grows as slowly as possible.
This approach is common in graph optimization problems involving connectivity constraints. It relies on ideas from sorting, efficient component merging using union find, and greedy edge processing often seen in graph algorithms.
Recommended for interviews: Interviewers expect the Sorting + Union-Find approach. The brute force threshold idea demonstrates understanding of the constraint, but the optimal solution shows you recognize the Kruskal-style greedy pattern and know how to implement efficient disjoint-set structures.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Threshold Simulation | O(E * (V + E)) | O(V) | Useful for understanding feasibility checks or very small graphs |
| Sorting + Union-Find (Greedy) | O(E log E) | O(V) | Best general solution for large graphs with connectivity constraints |