↓Skip to main content
  1. LeetCode/

LeetCode 1319: Number of Operations to Make Network Connected

·2 mins· ·
LeetCode Medium Union-Find
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-05-06

  • Total time: 20:00.00

Intuition
#

The minimum number of cables required to ensure all computers are connected is n - 1, where n is the number of computers.
If the number of available cables m is less than n - 1, it’s impossible to connect all computers.
So, the idea is to use Union Find to group all computers that are already connected.
After grouping, we can count how many independent components exist, which tells us how many additional cables are needed.
During the grouping process, if we try to union two computers that are already connected, we can record that cable as an extra cable.

At the end, if the number of extra cables is greater than or equal to the number of needed operations (independent groups minus one), we can return that number. Otherwise, return -1.

Approach
#

class Solution:
    def makeConnected(self, n: int, connections: List[List[int]]) -> int:
        parent = [i for i in range(n)]
        ranking = [0 for _ in range(n)]
        extra = 0
        needed = 0

        def find(n):
            if parent[n] != n:
                parent[n] = find(parent[n])

            return parent[n]

        def union(x, y):
            nonlocal extra
            root_x = find(x)
            root_y = find(y)

            if root_x == root_y:
                extra += 1
                return

            if ranking[root_x] > ranking[root_y]:
                parent[root_y] = parent[root_x]
                ranking[root_x] += ranking[root_y]

            else:
                parent[root_x] = parent[root_y]
                ranking[root_y] += ranking[root_x]

        for x, y in connections:
            union(x, y)

        for i in range(n):
            if parent[i] == i:
                needed += 1
        
        return needed - 1 if (needed-1)<=extra else -1

Findings
#

Encountered Problems
#

Related

LeetCode 1202: Smallest String With Swaps
·1 min
LeetCode Medium Union-Find
LeetCode Problem Solving
LeetCode 547: Number of Provinces
·3 mins
LeetCode Medium Union-Find Dfs
LeetCode Problem Solving
LeetCode 684: Redundant Connection
·1 min
LeetCode Daily Medium Union-Find
LeetCode Problem Solving
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 695: Max Area of Island
·2 mins
LeetCode Medium Array Depth-First-Search Breadth-First-Search Union-Find
LeetCode 695 note: DFS each unvisited 1 to measure its island and keep the maximum; cells are the integer 1, not a string
LeetCode 128: Longest Consecutive Sequence
·2 mins
LeetCode Medium Array Hash-Table Union-Find
LeetCode 128 note: put everything in a set and count upward only from values whose x - 1 is absent; each value is seen at most twice, O(n)