1
var minRefuelStops = function (target, startFuel, stations) {2
let pq = new MaxPriorityQueue({ compare: (a, b) => b[1] - a[1] });4
reachablePosition = startFuel,6
// reachablePosition is the farthest position reachable so far8
while (reachablePosition < target) {9
// While reachablePosition is >= to the current station's position, add the current station to the heap10
// These stations are all reachable based on the fuel available11
// Once reachablePosition is less than a station's position, the while loop ends12
while (idx < stations.length && reachablePosition >= stations[idx][0]) {13
pq.enqueue([stations[idx][0], stations[idx][1]]);16
// Next, add fuel from the heap in a greedy manner: the station with the most fuel gets added first17
// All stations in the heap are reachable, so the position is irrelevant19
let [pos, fuel] = pq.dequeue();20
reachablePosition += fuel;25
return reachablePosition >= target ? refuels : -1;