The O(n) Club: Minimum Refueling Stops—Out of Gas, Out of Excuses
⚡ TL;DR
Here’s the deal: you’ve got a half-tank, a desert, and a finite tolerance for junk food. Stations offer random amounts of fuel. Your mission? Stop the bare minimum times—because chewing cardboard beef jerky isn’t a lifestyle.
Brute force is for masochists. Max-heap is how adults win: keep rolling until you’re almost stranded, then chug the juiciest tank you’ve driven past. Repeat until Vegas (or your target) is achieved.// Java: Greedy max-heap on the go PriorityQueue <integer> pq = new PriorityQueue<>(Collections.reverseOrder()); int fuel = startFuel, stops = 0, i = 0; while (fuel < target) { while (i < stations.length && stations[i][0] <= fuel) pq.offer(stations[i++][1]); if (pq.isEmpty()) return -1; fuel += pq.poll(); stops++; } return stops; </integer>
🧠 How Devs Usually Mess This Up
🔍 What’s Actually Going On
Imagine you’re an anxious EV driver eyeing a dying battery. You only charge when you’re forced to. When stuck, you mentally double back and remember every juicy charging station you passed. You always take the biggest boost available (max-heap style), never a sip from a kiddie pool. That’s the vibe.
In actual code, this max-heap is your car’s “Nope, didn’t stop there (yet)” pile. You’re greedy, but strategic—always grabbing the best tank from what you’ve passed when you’re just about tapped out.
🛠️ PseudoCode
- Start with
fuel = startFuel,stops = 0, and an empty max-heap. - While fuel < target:
- Add every reachable station’s fuel to your max-heap (if station.position ≤ fuel).
- If max-heap is empty and you’re out of reach: return -1 (time for a walk).
- Else, guzzle the biggest available tank from the heap, increment stops.
// Heap pseudo-Java:
while (fuel < target) {
for (each station reachable) add station fuel to heap;
if (heap empty) return -1;
fuel += heap.poll();
stops++;
}
💻 The Code
public int minRefuelStops(int target, int startFuel, int[][] stations) {
PriorityQueue
<integer> heap = new PriorityQueue<>(Collections.reverseOrder());
int fuel = startFuel, stops = 0, i = 0;
while (fuel < target) {
while (i < stations.length && stations[i][0] <= fuel) {
heap.offer(stations[i][1]);
i++;
}
if (heap.isEmpty()) return -1;
fuel += heap.poll();
stops++;
}
return stops;
}</integer>
⚠️ Pitfalls, Traps & Runtime Smacks
- Trying to add fuel from that out-of-reach station? Good luck—your car isn’t Hermione’s purse.
- Missing edge stations just before the target. One off-by-one and you’re stranded on the off-ramp.
- Stations that offer zero gas: ignore them, unless you like doing pointless work.
- If
startFuel >= target, just return zero stops. Sadly, that’s the least fun test case. - Runtime: Feels zippy—O(n log n). Space: Heap size is at most the number of stations you’ve passed. Your RAM won’t run out before your patience does.
🧠 Interviewer Brain-Tattoo
“Don’t stop unless you must—and when you do, guzzle the biggest tank you’ve skipped. Greedy? Absolutely. But hey, it’s optimal.”