Min Cost to Connect All Points

MediumGraphs · Minimum Spanning Tree🧩 MST & Euler PathsLeetCode #1584 ↗Updated

Problem

You get n distinct points on a 2D plane. Linking two points costs their Manhattan distance |x1-x2| + |y1-y2|. Return the minimum total cost so that every point is connected to every other (directly or through other points).
Input[[0,0],[2,2],[3,10],[5,2],[7,0]]Output20
Input[[3,12],[-2,5],[-4,1]]Output18

1 ≤ n ≤ 1000  ·  -106 ≤ xi, yi ≤ 106  ·  all points distinct

Try it yourself

Java
class Solution {
    public int minCostConnectPoints(int[][] points) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
It is an MST on a complete graph (E ≈ n2/2): array-based Prim in O(n2) beats sorting or heaping the edges.
PatternMST & Euler Paths

Instinct "Connect every point for the least total cost" means a minimum spanning tree; "use every edge or ticket exactly once" means an Euler path.

Memory hack MST - grab the cheapest bridge that joins two separate islands. Euler - wander until stuck, write the stop down on the way back, then reverse.

Open the MST & Euler Paths guide →

Approaches

Brute ForceKruskal on All Pairs (Sort + Union-Find)

Time O(n2 log n)Space O(n2)

Materialise all n(n-1)/2 edges, sort by cost, and greedily take each edge that joins two different components (checked with Union-Find). By the cut property the cheapest edge across any cut is always safe; stop at n-1 edges.

Java
public int minCostConnectPoints(int[][] points) {
    int n = points.length, cost = 0, used = 0;
    List<int[]> edges = new ArrayList<>();
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++) {
            int d = Math.abs(points[i][0] - points[j][0]) + Math.abs(points[i][1] - points[j][1]);
            edges.add(new int[]{d, i, j});
        }
    edges.sort((x, y) -> x[0] - y[0]);
    int[] parent = IntStream.range(0, n).toArray(); // each point is its own set
    for (int[] e : edges) {
        int ra = find(parent, e[1]), rb = find(parent, e[2]);
        if (ra == rb) continue; // would close a cycle
        parent[ra] = rb;
        cost += e[0];
        if (++used == n - 1) break;
    }
    return cost;
}
private int find(int[] parent, int x) {
    return parent[x] == x ? x : (parent[x] = find(parent, parent[x]));
}

Classic and correct, but stores and sorts ~500k edges for n = 1000.

At a glance

LevelApproachTimeSpace
Brute ForceKruskal on All Pairs (Sort + Union-Find)O(n2 log n)O(n2)
BetterPrim with a Min-HeapO(n2 log n)O(n2)
OptimalPrim with a Plain Array (Dense Graph)O(n2)O(n)
Common pitfalls
  • Forgetting if (inTree[u]) continue in heap Prim — double-counts cost.
  • Using Euclidean instead of Manhattan distance.
  • Running Dijkstra (path sums) instead of Prim (single edge cost).
Interview tips
  • Name it immediately: “connect everything at min total cost” = MST.
  • Point out E = O(n2), which is why array Prim wins here.
  • Know both Kruskal and Prim; justify them with the cut property.
Pattern
Minimum Spanning Tree → link all nodes with the least total edge weight, no cycles needed.
My notes saved in this browser