Min Cost to Connect All Points

You are given points, a list of distinct [x, y] coordinates on a plane. Connecting two points costs their Manhattan distance: |x1 - x2| + |y1 - y2|.

Choose a set of connections so that every point can reach every other point (directly or through other points), and return the smallest possible total cost of those connections. There is no need to connect every pair: only enough connections to make one connected network.

Constraints

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

Examples

Example 1

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

Connect (0,0)-(2,2) for 4, (2,2)-(5,2) for 3, (5,2)-(7,0) for 4, and (2,2)-(3,10) for 9. The total is 20.

Example 2

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

(-4,1)-(-2,5) costs 6 and (-2,5)-(3,12) costs 12: total 18.

Example 3

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

One point is already connected: cost 0.

JavaScript you'll use: Classes for data structures Sorting and comparators

/**
 * @param {number[][]} points
 * @return {number}
 */
function minCostConnectPoints(points) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.