Min Cost to Connect All Points
Problem Statement
You are given n points on a 2D plane, where points[i] = [xᵢ, yᵢ].
Link the points so that every point can reach every other point through exactly one path. The cost of linking two points is their Manhattan distance: |xᵢ − xⱼ| + |yᵢ − yⱼ|. Any two points can be linked, so every pair is a possible link.
Return the minimum total cost to link all the points.
points, a list of [x, y] coordinates.
A single integer: the minimum total cost to link all the points.
Link 0–1 (4), 1–3 (3), 3–4 (4) and 1–2 (9). That is 4 + 3 + 4 + 9 = 20, and no cheaper set of links reaches every point.
Link points 2 and 1 (cost 6) and points 0 and 1 (cost 12). That is 6 + 12 = 18.
- • 1 ≤ points.length ≤ 1000
- • −10^6 ≤ xᵢ, yᵢ ≤ 10^6
- • All points are distinct.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Grow the tree one point at a time, scanning every (inside, outside) pair each round
List every pair as an edge, sort them once, keep a pair only if its points are in different groups
Keep dist[j], the cheapest link from the tree to point j; each round adds the minimum and updates only that point's links
Start the tree at point 0. In each of the n − 1 rounds, scan every pair where one point is in the tree and the other is outside. Add the outside point of the cheapest pair, and add its cost to the total.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Union-Find (DSU) translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.