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.