You are given an array of distinct points on a 2D plane, where each point is written as [x, y]. The cost of connecting two points equals the Manhattan distance between them, that is |x1 - x2| + |y1 - y2|.
Every point must end up connected to every other point, directly or through intermediate points, and there must be exactly one simple path between any two points. Return the minimum total cost that achieves this.
Example 1
Input: points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
Output: 20
Connecting the points with the edges of total weight 20 links every point while keeping the sum of Manhattan distances as small as possible.
Example 2
Input: points = [[3,12],[-2,5],[-4,1]]
Output: 18
Constraints
1 <= points.length <= 1000-10^6 <= xi, yi <= 10^6All pairs (xi, yi) are distinct.See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI