Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 1584 · Minimum Spanning Tree

Min Cost to Connect All Points

LeetCode 1584 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Kruskal: sorted edges + union-find.

ಮಧ್ಯಮArrayUnion-FindGraph TheoryMinimum Spanning TreePrim's Algorithm
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Min Cost to Connect All Points (LeetCode 1584) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Minimum Spanning Tree ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Connecting every point as cheaply as possible is a minimum spanning tree. Kruskal's algorithm sorts all candidate edges by Manhattan distance and adds each one that connects two different components, tracked by a disjoint set.

Approach

  1. 1

    Build every pair's Manhattan distance.

  2. 2

    Sort edges by weight.

  3. 3

    Union endpoints in different components and add the weight.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Kruskal + DSUPythonO(n² log n) time
class DisjointSet:
     def __init__(self,n):
         self.rank = [0]*(n+1)
         self.parents = [i for i in range(n+1)]
     def find(self,u):
         if self.parents[u]==u:
             return u
         self.parents[u]=self.find(self.parents[u])
         return self.parents[u]
     def union(self,u,v):
        ulp_u,ulp_v = self.find(u),self.find(v)
        if ulp_u==ulp_v:
            return 
        if self.rank[ulp_u]<self.rank[ulp_v]:
            self.parents[ulp_u]=ulp_v
        elif self.rank[ulp_v]<self.rank[ulp_u]:
            self.parents[ulp_v]=ulp_u
        elif self.rank[ulp_v]==self.rank[ulp_u]:
            self.parents[ulp_v]=ulp_u
            self.rank[ulp_u]+=1 
class Solution:
    def minCostConnectPoints(self, points: List[List[int]]) -> int:
        n= len(points)
        ds = DisjointSet(n)
        edges = []
        for i in range(n):
            for j in range(i+1,n):
                xi,yi = points[i]
                xj,yj = points[j]
                wt = abs(xi - xj) + abs(yi - yj)
                edges.append([wt,i,j])
        edges.sort(key=lambda x:x[0])
        weight = 0
        for w,u,v in edges:
            if ds.find(u)!=ds.find(v):
                ds.union(u,v)
                weight+=w
        return weight

        
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Min Cost to Connect All Points — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Min Cost to Connect All Points ಹಿಂದಿನ intuition ಏನು?

Connecting every point as cheaply as possible is a minimum spanning tree. Kruskal's algorithm sorts all candidate edges by Manhattan distance and adds each one that connects two different components, tracked by a disjoint set.

Min Cost to Connect All Points (LeetCode 1584) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Build every pair's Manhattan distance. 2. Sort edges by weight. 3. Union endpoints in different components and add the weight.

Min Cost to Connect All Points ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(n² log n). Space: O(n²). (Accepted approach: Kruskal + DSU.)

Min Cost to Connect All Points ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

Prim's with a heap avoids storing all n² edges.