3
The approach here used is - first cosider the stations which are possible at a4
particular time and then take the one with maximum fuel that can be filled this5
ensure that with only one fill up the vehicle will move to furthest distance.7
Now there might be case where if we don't fill up from multiple stations ata8
paritcular time, then it will not be possible to reach end as there might not be9
any stations at the ending side of the target.11
To handle above case, when such situation arise then we need to take the next12
max fuel refill that is pissible from the already seen stations. Like this way13
keep on taking fuels from stations in decreasing manner.15
If at any point of time the queue is empty then, it means we do not have16
sufficent fuel to reach target.18
=> This logic can be implemented by simply using a priority queue where the max19
fuel station is at top of the queue.26
int minRefuelStops(int target, int startFuel, vector<vector<int>> &stations) {27
auto comp = [](vector<int> a, vector<int> b) { return a[1] < b[1]; };28
priority_queue<vector<int>, vector<vector<int>>, decltype(comp)> pq(comp);29
int i = 0, distance = startFuel, refillCount = 0;30
while (distance < target) {31
while (i < stations.size() && distance >= stations[i][0]) {32
pq.push(stations[i++]);34
if (pq.empty()) return -1;35
distance += pq.top()[1];