Connect All Points
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
Write minCostConnect(points). points is an array of different [x, y] pairs of whole numbers. Joining two points costs their Manhattan distance: the difference between their x values plus the difference between their y values, both taken as positive. Return the lowest total cost that connects every point, directly or through other points, which is the weight of a minimum spanning tree. One point costs 0. For example, minCostConnect([[0, 0], [2, 2], [3, 10], [5, 2], [7, 0]]) is 20. The checks include 2,500 points, with over three million pairs between them, so sorting every pair is too slow: grow the tree one point at a time instead (Prim's algorithm).
Your code must pass
- Finds the cheapest way to connect the points
- Links through other points, not just to the first
- Handles one point and two points
- Is fast enough for 2,500 points