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

मी 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.