Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 1319 · Union Find / DSU

Number of Operations to Make Network Connected

LeetCode 1319 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Count extra edges and components with DSU.

நடுத்தரம்Depth-First SearchBreadth-First SearchUnion-FindGraph Theory
LeetCode இல் திற ↗

நான் Number of Operations to Make Network Connected (LeetCode 1319) ஐ இப்படித் தீர்த்தேன் — Union Find / DSU பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Every cable that connects two already-connected computers is spare. To join k components you need k − 1 cables, so it's possible exactly when spare cables ≥ components − 1.

Approach

  1. 1

    Union each connection; already connected → extras++.

  2. 2

    Count components.

  3. 3

    required = components − 1; return it if extras suffice, else −1.

நான் எப்படித் தீர்த்தேன்

DSUPythonO(E α(n)) time
class DisjointSet:
    def __init__(self,n):
        self.rank = [0]*(n+1)
        self.parent = [i for i in range(n+1)]
    def find(self,u):
        if self.parent[u]==u:
            return u
        self.parent[u]=self.find(self.parent[u])
        return self.parent[u]
    def union(self,u,v):
        pu,pv = self.find(u),self.find(v)
        if pu==pv:
            return 
        if self.rank[pu]<self.rank[pv]:
            self.parent[pu]=pv
        elif self.rank[pv]<self.rank[pu]:
            self.parent[pv]=pu
        elif self.rank[pv]==self.rank[pu]:
            self.parent[pv]=pu
            self.rank[pu]+=1


class Solution:
    def makeConnected(self, n: int, connections: List[List[int]]) -> int:
       ds = DisjointSet(n)
       extras=0
       for u,v in connections:
            if ds.find(u)==ds.find(v):
                extras+=1
            else:
                ds.union(u,v)
       components = 0
       for u in range(n):
        if ds.parent[u]==u:
            components+=1
       required = components-1
       if required<=extras:
            return required
       else:
            return -1
        
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Number of Operations to Make Network Connected — அடிக்கடி கேட்கப்படும் கேள்விகள்

Number of Operations to Make Network Connected பின்னாலுள்ள intuition என்ன?

Every cable that connects two already-connected computers is spare. To join k components you need k − 1 cables, so it's possible exactly when spare cables ≥ components − 1.

Number of Operations to Make Network Connected (LeetCode 1319) ஐ எப்படித் தீர்ப்பது?

1. Union each connection; already connected → extras++. 2. Count components. 3. required = components − 1; return it if extras suffice, else −1.

Number of Operations to Make Network Connected இன் time மற்றும் space complexity என்ன?

Time: O(E α(n)). Space: O(n). (Accepted approach: DSU.)

Number of Operations to Make Network Connected இல் பொதுவான தவறுகள் என்ன?

Fewer than n − 1 cables total means −1 immediately.