↓Skip to main content
  1. LeetCode/

LeetCode 3613: Minimize Maximum Component Cost

·2 mins· ·
LeetCode Medium Union-Find Graph Sorting Binary-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-07-13

  • Submissions: Wrong Answer → Accepted

Approach
#

An answer W means “keeping only edges with weight <= W leaves <= k components”. More edges mean fewer components, so add edges from light to heavy (Kruskal, Union-Find edges sorted by weight); the weight when the count first reaches k is the smallest W.

  • Only an edge that actually merges two components reduces the count; edges inside a component do not affect the answer.
  • With n <= k the condition already holds; remove every edge and the answer is 0. The graph is guaranteed connected, so the count always reaches k.

My Attempts
#

Attempt 1 missed n == k: the count starts at k, every merge goes below it, the loop never sees exactly k, and nothing is returned. For n = 1, edges = [], k = 1 the answer is 0; it returned None. Adding the special case passed.

Solution
#

class Solution:
    def minCost(self, n: int, edges: List[List[int]], k: int) -> int:
        if n <= k:
            return 0
        parent = list(range(n))

        def find(x):
            while parent[x] != x:
                parent[x] = parent[parent[x]]
                x = parent[x]
            return x

        groups = n
        for u, v, w in sorted(edges, key=lambda e: e[2]):
            ru, rv = find(u), find(v)
            if ru != rv:
                parent[ru] = rv
                groups -= 1
                if groups == k:
                    return w

Complexity
#

Time O(E log E + E · α(n)), space O(n).

Mistakes to Avoid
#

  • Handle the case that already satisfies the condition first.
  • When checking groups == k, make sure it drops by exactly 1 each time and must pass through k.

Interview Explanation
#

I add edges to Union-Find from light to heavy, decrementing the count on each real merge. When exactly k components first remain, the heaviest edge used is the current one, so its weight is the answer. With n <= k no edge is needed, so return 0. Time O(E log E).

Related

LeetCode 3608: Minimum Time for K Connected Components
·2 mins
LeetCode Medium Union-Find Graph Sorting Binary-Search
LeetCode 3608 note: add edges back from the largest time; the time of the first edge that drops the component count below k is the answer
LeetCode 3607: Power Grid Maintenance
·3 mins
LeetCode Medium Heap Union-Find Graph Lazy-Deletion
LeetCode 3607 note: Union-Find groups, one min-heap per group, and lazy deletion for offline stations
LeetCode 3493: Properties Graph
·2 mins
LeetCode Medium Graph Dfs Union-Find Hash-Map
LeetCode 3493 note: convert each row to a set, compare intersection sizes pairwise to add edges, then count components
LeetCode 164: Maximum Gap
·3 mins
LeetCode Medium Bucket-Sort Radix-Sort Sorting Array
LeetCode 164 note: the maximum gap is at least the average gap, so with buckets no wider than that it lies between consecutive non-empty buckets; keep each bucket’s min and max for O(n)
LeetCode 3508: Implement Router
·3 mins
LeetCode Medium Design Binary-Search Queue Hash-Map
LeetCode 3508 note: a queue for FIFO, a set for duplicates, and an increasing time list per destination queried with bisect
LeetCode 1353: Maximum Number of Events That Can Be Attended
·2 mins
LeetCode Medium Heap Greedy Sorting
LeetCode 1353 note: each day, attend the event that ends earliest, using a min-heap of end days