Skip to main content
  1. LeetCode/

LeetCode 1319: Number of Operations to Make Network Connected

·2 mins· ·
LeetCode Union Find Medium
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 Union Find Medium
LeetCode Problem Solving
LeetCode 547: Number of Provinces
·3 mins
LeetCode Union Find DFS Medium
LeetCode Problem Solving
LeetCode 684: Redundant Connection
·1 min
LeetCode Union Find Medium
LeetCode Problem Solving
LeetCode 1094: Car Pooling
·2 mins
LeetCode Difference Array Medium
LeetCode Problem Solving
LeetCode 1109: Corporate Flight Bookings
·1 min
LeetCode Difference Array Medium
LeetCode Problem Solving
LeetCode 3527: Find the Most Common Response
·1 min
LeetCode Bi-Weekly Medium
LeetCode Problem Solving