Skip to main content

Minimize Maximum Component Cost - Solution & Explanation

MediumBinary SearchUnion FindGraphSorting10 min readAsked at: Microsoft, Apple, Salesforce +1
Practice this problem

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.

 

Example 1:

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

Output: 4

Explanation:

  • Remove the edge between nodes 3 and 4 (weight 6).
  • The resulting components have costs of 0 and 4, so the overall maximum cost is 4.

Example 2:

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

Output: 5

Explanation:

  • No edge can be removed, since allowing only one component (k = 1) requires the graph to stay fully connected.
  • That single component’s cost equals its largest edge weight, which is 5.

 

Constraints:

  • 1 <= n <= 5 * 104
  • 0 <= edges.length <= 105
  • edges[i].length == 3
  • 0 <= ui, vi < n
  • 1 <= wi <= 106
  • 1 <= k <= n
  • The input graph is connected.

Approach Overview

Problem 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.

Solution

If k = n, it means all edges can be removed. In this case, all connected components are isolated nodes, and the maximum cost is 0.

Otherwise, we can sort all edges by weight in ascending order, then use a union-find data structure to maintain connected components.

We assume that initially all nodes are not connected, with each node being an independent connected component. Starting from the edge with the smallest weight, we try to add it to the current connected components. If the number of connected components becomes less than or equal to k after adding the edge, it means all remaining edges can be removed, and the weight of the current edge is the maximum cost we are looking for. We return this weight. Otherwise, we continue processing the next edge.

The time complexity is O(n times log n), and the space complexity is O(n), where n is the number of nodes.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Threshold SimulationO(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

Video Solution

Leetcode 3613 | Minimize Maximum Component Cost Detailed Explanation | Q2 Leetcode contest 458Samrat Bhardwaj618 views views

Watch 6 more video solutions →

Frequently Asked Questions

Is Minimize Maximum Component Cost easy or hard?
The problem is generally rated Medium because it requires recognizing the greedy pattern and implementing Union-Find correctly. Developers familiar with minimum spanning tree techniques usually identify the solution quickly. Beginners may struggle with component tracking and DSU optimizations.
Minimize Maximum Component Cost Python/Java solution
Most implementations follow the same pattern: sort edges, initialize a Union-Find structure, and iterate through edges performing union operations. Python typically uses a parent array with path compression, while Java implementations use class-based DSU structures. Both achieve O(E log E) time complexity.
How to solve Minimize Maximum Component Cost in O(E log E)?
Sort all edges by their cost and iterate through them in ascending order. Use a Union-Find structure to merge components whenever two vertices belong to different sets. Since edges are processed from smallest to largest, the maximum cost introduced into any component stays minimized.
What is the best approach for Minimize Maximum Component Cost?
The most effective approach is sorting edges by cost and merging components using Union-Find. This greedy strategy ensures smaller costs are processed first, preventing unnecessary increases in the maximum component cost. The method runs in O(E log E) time due to sorting and nearly constant-time union operations.
Is Minimize Maximum Component Cost asked at Google/Amazon/Meta?
Graph connectivity problems using Union-Find appear frequently in interviews at companies like Google, Amazon, and Meta. Variants often resemble minimum spanning tree or component-merging optimization tasks. Recognizing the Kruskal-style greedy pattern is commonly expected.
What data structure is used in Minimize Maximum Component Cost?
The core data structure is Union-Find (Disjoint Set Union). It tracks connected components efficiently and supports near-constant-time merging with path compression and union by rank. Sorting is also required to process edges in increasing cost order.
What is the time complexity of Minimize Maximum Component Cost?
The optimal solution runs in O(E log E) time because the edges must be sorted before processing. Union-Find operations such as union and find run in amortized O(α(V)) time with path compression and union by rank, making them effectively constant. Space complexity is O(V) for the disjoint-set structure.

Ready to solve this problem?

Practice Minimize Maximum Component Cost with our built-in code editor and test cases.

Practice on FleetCode