Paths Subjects Questions Quizzes Pricing Search
Advanced Open Pro

Connect the Dots

You are given n points on a 2D plane, points[i] = [xi, yi]. The cost to connect two points i and j directly is their Manhattan distance: |xi - xj| + |yi - yj|. Return the minimum total cost to connect all n points so that there is exactly one path between any pair of points (i.e., build a minimum spanning tree over the points, where every pair of points is a potential edge with that weight).

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

Constraints

  • 1 <= points.length <= 1000
  • -10^6 <= xi, yi <= 10^6
  • All pairs (xi, yi) are distinct.

Share this question

← Back to Graphs practice

We use cookies for product analytics to improve OmniAtlas. See our Privacy Policy.