Skip to content
Decoder

Min Cost to Connect All Points

Medium

Problem

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.

Example 1

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

Example 2

Input
points = [[3, 12], [-2, 5], [-4, 1]]
Output
18

Example 3

Input
points = [[0, 0]]
Output
0

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.