A car travels from a starting position to a destination which is target miles east of the starting position.

There are gas stations along the way. The gas stations are represented as an array stations where stations[i] = [positioni, fueli] indicates that the ith gas station is positioni miles east of the starting position and has fueli liters of gas.

The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it. It uses one liter of gas per one mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.

Return the minimum number of refueling stops the car must make in order to reach its destination. If it cannot reach the destination, return -1.

Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there. If the car reaches the destination with 0 fuel left, it is still considered to have arrived.

&nbsp;
Example 1:

Input: target = 1, startFuel = 1, stations = []
Output: 0
Explanation: We can reach the target without refueling.


Example 2:

Input: target = 100, startFuel = 1, stations = [[10,100]]
Output: -1
Explanation: We can not reach the target (or even the first gas station).


Example 3:

Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
Output: 2
Explanation: We start with 10 liters of fuel.
We drive to position 10, expending 10 liters of fuel.  We refuel from 0 liters to 60 liters of gas.
Then, we drive from position 10 to position 60 (expending 50 liters of fuel),
and refuel from 10 liters to 50 liters of gas.  We then drive to and reach the target.
We made 2 refueling stops along the way, so we return 2.


&nbsp;
Constraints:


	1 &lt;= target, startFuel &lt;= 109
	0 &lt;= stations.length &lt;= 500
	0 &lt;= positioni &lt;= positioni+1 &lt; target
	1 &lt;= fueli &lt; 109


class Solution:
    def minRefuelStops(self, t, F, S):
        S.append([t, 0])
        heap, ans = [], 0
        for p,f in S:
            while heap and p > F:
                F -= heapq.heappop(heap)
                ans += 1
            if p > F: return -1
            heapq.heappush(heap, -f)
        return ans
class Solution {
    public int minRefuelStops(int target, int startFuel, int[][] stations) {
        if(startFuel >= target) return 0;
        int[][] dp = new int[stations.length + 1][stations.length + 1];
        for (int i = 0; i < dp.length; i++) dp[i][0] = startFuel;
        for (int j = 1; j < dp.length; j++) {
            for (int i = j; i < dp.length; i++) {
                dp[i][j] = Math.max(dp[i-1][j], stations[i-1][0] > dp[i-1][j-1] ?
                        Integer.MIN_VALUE : dp[i-1][j-1] + stations[i-1][1]);
                if(dp[i][j] >= target) return j;
            }
        }
        return -1;
    }
}
/*

The approach here used is - first cosider the stations which are possible at a particular time and then take the one with maximum fuel that can be filled this ensure that with only one fill up the vehicle will move to furthest distance.

Now there might be case where if we don't fill up from multiple stations ata paritcular time, then it will not be possible to reach end as there might not be any stations at the ending side of the target. 

To handle above case, when such situation arise then we need to take the next max fuel refill that is pissible from the already seen stations.
Like this way keep on taking fuels from stations in decreasing manner.

If at any point of time the queue is empty then, it means we do not have sufficent fuel to reach target.

=> This logic can be implemented by simply using a priority queue where the max fuel station is at top of the queue.


*/

class Solution {
public:
    int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
        auto comp = [](vector<int> a, vector<int> b){ return a[1] < b[1]; };
        priority_queue<vector<int>, vector<vector<int>>, decltype(comp)> pq(comp);
        int i = 0, distance = startFuel, refillCount = 0;
        while(distance < target ){
            while(i < stations.size() && distance >= stations[i][0]){
                pq.push(stations[i++]);
            }
            if(pq.empty()) return -1;
            distance += pq.top()[1];
            refillCount++;
            pq.pop();
        }
        return refillCount;
    }
};
var minRefuelStops = function(target, startFuel, stations) {
    let pq = new MaxPriorityQueue({compare: (a, b) => b[1] - a[1]});
    let idx = 0, reachablePosition = startFuel, refuels = 0;
    // reachablePosition is the farthest position reachable so far
    
    while (reachablePosition < target) {
        // While reachablePosition is >= to the current station's position, add the current station to the heap
        // These stations are all reachable based on the fuel available
        // Once reachablePosition is less than a station's position, the while loop ends
        while (idx < stations.length && reachablePosition >= stations[idx][0]) {
            pq.enqueue([stations[idx][0], stations[idx][1]]);
            idx++;
        }
        // Next, add fuel from the heap in a greedy manner: the station with the most fuel gets added first
        // All stations in the heap are reachable, so the position is irrelevant 
        if (pq.size()) {
            let [pos, fuel] = pq.dequeue();
            reachablePosition += fuel;
            refuels++;
        } else break;
    }
    
    return reachablePosition >= target ? refuels : -1;
};
Minimum Number of Refueling Stops