|
| 1 | +/* |
| 2 | +Minimum Number of Refueling Stops |
| 3 | +================================= |
| 4 | +
|
| 5 | +A car travels from a starting position to a destination which is target miles east of the starting position. |
| 6 | +
|
| 7 | +Along the way, there are gas stations. Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. |
| 8 | +
|
| 9 | +The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it. It uses 1 liter of gas per 1 mile that it drives. |
| 10 | +
|
| 11 | +When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. |
| 12 | +
|
| 13 | +What is the least number of refueling stops the car must make in order to reach its destination? If it cannot reach the destination, return -1. |
| 14 | +
|
| 15 | +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. |
| 16 | +
|
| 17 | +Example 1: |
| 18 | +Input: target = 1, startFuel = 1, stations = [] |
| 19 | +Output: 0 |
| 20 | +Explanation: We can reach the target without refueling. |
| 21 | +
|
| 22 | +Example 2: |
| 23 | +Input: target = 100, startFuel = 1, stations = [[10,100]] |
| 24 | +Output: -1 |
| 25 | +Explanation: We can't reach the target (or even the first gas station). |
| 26 | +Example 3: |
| 27 | +Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] |
| 28 | +Output: 2 |
| 29 | +Explanation: |
| 30 | +We start with 10 liters of fuel. |
| 31 | +We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. |
| 32 | +Then, we drive from position 10 to position 60 (expending 50 liters of fuel), |
| 33 | +and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. |
| 34 | +We made 2 refueling stops along the way, so we return 2. |
| 35 | +
|
| 36 | +Note: |
| 37 | +1 <= target, startFuel, stations[i][1] <= 10^9 |
| 38 | +0 <= stations.length <= 500 |
| 39 | +0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target |
| 40 | +*/ |
| 41 | + |
| 42 | +class Solution |
| 43 | +{ |
| 44 | +public: |
| 45 | + int minRefuelStops(int dest, int fuel, vector<vector<int>> &arr) |
| 46 | + { |
| 47 | + arr.push_back({dest, 0}); |
| 48 | + int ans = 0, prev = 0; |
| 49 | + |
| 50 | + priority_queue<int> canFill; |
| 51 | + |
| 52 | + for (int i = 0; i < arr.size(); ++i) |
| 53 | + { |
| 54 | + int dist = arr[i][0] - prev; |
| 55 | + prev = arr[i][0]; |
| 56 | + |
| 57 | + if (fuel >= dist) |
| 58 | + { |
| 59 | + // we had sufficient fuel previously to come till here |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + // mtlb fuel km tha previously, |
| 64 | + // so to come here he must have refilled at previously visited station |
| 65 | + while (canFill.size() && fuel < dist) |
| 66 | + { |
| 67 | + fuel += canFill.top(); |
| 68 | + canFill.pop(); |
| 69 | + ans++; |
| 70 | + } |
| 71 | + |
| 72 | + if (fuel < dist) |
| 73 | + return -1; |
| 74 | + } |
| 75 | + |
| 76 | + fuel -= dist; |
| 77 | + canFill.push(arr[i][1]); |
| 78 | + } |
| 79 | + return ans; |
| 80 | + } |
| 81 | +}; |
0 commit comments