28 · Union-Find & Minimum Spanning Trees

#1584 Min Cost to Connect All Points

Mediumsolve on LeetCode ↗

frequently reported atAmazonGoogleMicrosoft

the problem

In plain words

Given points on a 2D plane, the cost to connect two points is their Manhattan distance |x₁ − x₂| + |y₁ − y₂|. Return the minimum cost to connect all points so that every pair has exactly one simple path between them.

in points = [[0,0],[2,2],[3,10],[5,2],[7,0]]

out 20

in points = [[3,12],[-2,5],[-4,1]]

out 18

  • 1 ≤ points.length ≤ 1000
  • -10⁶ ≤ xᵢ, yᵢ ≤ 10⁶
  • All points are distinct

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #1489
    2. #1631
      Path With Minimum Effort
      Mediumin Shortest Paths
    3. #743
      Network Delay Time
      Mediumin Shortest Paths