Min Cost to Connect All Points

Problem

Given the coordinates of points on a 2D plane, return the minimum total cost to connect all of them, where the cost to connect two points is their Manhattan distance — effectively building a minimum spanning tree.

Examples

Example 1
Input:points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
Output:20

Constraints

  • 1 <= points.length <= 1000

Solve it in the editor. Sign in free to run your Python or JavaScript against test cases, get a verdict, and track your attempts.

Solve on FeatCode →

How to approach it: the Advanced Graphs pattern

Beyond basic traversal, some graph problems need ordering constraints (topological sort) or weighted shortest paths (Dijkstra, Bellman-Ford, minimum spanning trees).

Look for this pattern when

  • The problem has ordering or prerequisite constraints ("do X before Y").
  • Edges have weights or costs and you need the cheapest path, not just any path.

Read the full Advanced Graphs guide →

Video walkthroughs

Original problem on LeetCode ↗

More Advanced Graphs problems