This practice workspace is built for desktop. Open it on a larger screen to start solving.
Min Cost to Connect All Points
You are given an array points of n 2-D integer points. The cost to connect two points (x1, y1) and (x2, y2) is their Manhattan distance: |x1 - x2| + |y1 - y2|. Return the minimum cost to connect all points (i.e., build a minimum spanning tree).
Use Prim's algorithm: maintain a minCost array of the cheapest edge connecting each unvisited node to the current MST. At each step, pick the unvisited node with the smallest minCost, add it to the MST, then update neighbors.
Examples
Constraints
- 1 <= points.length <= 1000
- -(10^6) <= xi, yi <= 10^6
- All pairs (xi, yi) are distinct.
Preview Mode
This is a read-only preview of DSAMind's AI coaching. Sign up to get custom feedback on your own solution.
Your approach
- Pattern:Graphs
- Time:O(N)
- Space:O(1)
Complete approach — pattern and complexity both named.
Coach
What you got right
You correctly matched the sorted input requirement and implemented a linear-time scan using two pointers.
Where it diverged
No divergence detected. The code correctly aligns with the stated approach.
Next attempt: focus on
Try solving related sliding window problems to build familiarity with two-pointer variants.
Signals
No signals fired — clean run, you stayed in flow.