Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Advanced Graphs

Min Cost to Connect All Points

MEDIUM✓ CHIP-TIMEDLC #1584 — FULL STATEMENT

The briefingthe full drill, in my own words

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.

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.

  • point count can run into the low thousands
  • coordinates are integers, can be negative
  • cost between any two points is their Manhattan distance
  • output is a single total cost, not the list of chosen wires
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 hintstake 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 boardthe 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 splitstwo paces, two languages

grind/min-cost-to-connect-all-points.py — RACE 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-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED