◀ THE GRIND — ADVANCED GRAPHS

Min Cost to Connect All Points

The drill: Wire every point on the plane into one network, where the cost of a wire is the Manhattan distance between its two ends. Spend the least total wire that still leaves everything connected.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

A set of points on a 2D plane arrives, and any two of them can be joined by a direct wire. The cost of a wire between two points is their Manhattan distance — the sum of the horizontal and vertical gaps between them.

The goal is to choose a set of wires that leaves every point reachable from every other point, using the least total wire cost possible. Extra wires that don't help connectivity are pure waste.

There's no requirement on which specific wires get chosen — only that the whole set of points ends up in one connected network for the smallest total spend.

EX 01
points = [[0, 0]]
0
SINGLE POINT, NOTHING TO CONNECT
EX 02
points = [[0, 0], [3, 4]]
7
TWO POINTS, ONE EDGE
EX 03
points = [[0, 0], [2, 2], [3, 10], [5, 2], [7, 0]]
20
SELF-AUTHORED 5-POINT SPREAD
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

Any set of wires that connects everyone without a single redundant loop is a spanning tree. There are exponentially many of them — the question is only which one is cheapest.

HINT 2 THE STRUCTURE

Grow the network one point at a time: always attach whichever outside point is currently closest to the network you've already built. That greedy rule provably never needs to undo a choice.

HINT 3 ONE STEP FROM THE ANSWER

Track, for every point not yet in the tree, its current cheapest distance to the tree. Each round, pull in the smallest of those, then update the rest against the newly added point — n rounds, n candidates scanned each round.

COACH'S BOARD — THE PATTERN, STEP BY STEP
WIRING THE CHEAPEST NETPATTERN · PRIM'S, ARRAY NOT HEAPpoints: (0,0) (2,2) (3,10) (5,2) (7,0)
STEP 1

5 points, Prim's MST: start the tree at point 0 and repeatedly pull in whichever unconnected point is cheapest to reach — a plain array scan, no heap needed.

STEP 1 / 7 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/min-cost-to-connect-all-points.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def minCostConnectPoints(self, points: List[List[int]]) -> int:
        n = len(points)
        if n <= 1:
            return 0
        in_mst = [False] * n
        min_dist = [float('inf')] * n
        min_dist[0] = 0
        total = 0
        for _ in range(n):
            u = -1
            best = float('inf')
            for i in range(n):
                if not in_mst[i] and min_dist[i] < best:
                    best = min_dist[i]
                    u = i
            in_mst[u] = True
            total += best
            for v in range(n):
                if not in_mst[v]:
                    d = abs(points[u][0] - points[v][0]) + abs(points[u][1] - points[v][1])
                    if d < min_dist[v]:
                        min_dist[v] = d
        return total
TIME O(V²)SPACE O(V)PYTHON · RACE PACE · 24 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED