Skip to content

Min Cost to Connect All Points

MediumGraphs
Decode it first. Clues, constraints, pattern and plan before any code.

You get points on a 2D plane, each [x, y]. Connecting two points costs their Manhattan distance, |x1 - x2| + |y1 - y2|. Return the minimum total cost to connect all the points, where connected means there is exactly one simple path between any two of them.

points = [[0, 0], [2, 2], [3, 10], [5, 2], [7, 0]]  →  20
points = [[3, 12], [-2, 5], [-4, 1]]                →  18
points = [[0, 0]]                                   →  0

Constraints

  • 1 <= points.length <= 1000
  • -10^6 <= x, y <= 10^6
  • All points are distinct