Min Cost to Connect All Points
Given points array where points[i] = [x, y] Cost = Manhattan distance |x1-x2| + |y1-y2| Return minimum cost to connect all points Example: [[0,0],[2,2],[3,10],[5,2],[7,0]] => 20
Min Cost to Connect All Points is about always pulling the next best option efficiently as the data changes. Interviewers love it because it reveals your priority-based reasoning and your edge-case discipline. If your heap represents best candidates right now, the rest falls into place.
Examples
You write
min_cost_connect_points(points: List[List[int]])
Example 1
- Input
- points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
- returns
- 20
Example 2
- Input
- points = [[3,12],[-2,5],[-4,1]]
- returns
- 18
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problemsGraphs
I reach for it when
- The input is edges, prerequisites or connections.
- You need cycles, an ordering or connected groups.
Say this before you type. Say whether the edges are directed, and which of the three jobs you are doing.
All 32 Graphs problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.