The problem. You're given points[i] = [x, y] on a plane. Joining two points costs their Manhattan distance, |x₁ - x₂| + |y₁ - y₂|. Return the minimum total cost to connect all the points, so that there's exactly one route between any two of them.
points = [[0,0],[2,2],[3,10],[5,2],[7,0]] -> 20
points = [[3,12],[-2,5],[-4,1]] -> 18
points = [[0,0]] -> 0 (nothing to connect)Members only · free account
The rest of this lesson, with 6 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson