|
| 1 | +# Minimum Number of Refueling Stops |
| 2 | + |
| 3 | +You need to find the minimum number of refueling stops that a car needs to make to cover a distance, target. For |
| 4 | +simplicity, assume that the car has to travel from west to east in a straight line. There are various fuel stations on |
| 5 | +the way that are represented as a 2-D array of stations, i.e., stations[i]=[di,fi], where di is the distance (in miles) |
| 6 | +of the ith gas station from the starting position, and fi is the amount of fuel (in liters) that it stores. Initially, |
| 7 | +the car starts with k liters of fuel. The car consumes one liter of fuel for every mile traveled. Upon reaching a gas |
| 8 | +station, the car can stop and refuel using all the petrol stored at the station. If it cannot reach the target, the |
| 9 | +program returns −1. |
| 10 | + |
| 11 | +> Note: If the car reaches a station with 0 fuel left, it can refuel from that station, and all the fuel from that |
| 12 | +> station can be transferred to the car. If the car reaches the target with 0 fuel left, it is still considered to have |
| 13 | +> arrived. |
| 14 | +
|
| 15 | +## Constraints |
| 16 | + |
| 17 | +- 1 <= `target`, `k` <= 10^9 |
| 18 | +- 0 <= `stations.length`, <= 900 |
| 19 | +- 1 <= di < di+1 < `target` |
| 20 | +- 1 <= fi < 10^9 |
| 21 | + |
| 22 | +## Examples |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +## Solutions |
| 34 | + |
| 35 | +### Using Max Heap |
| 36 | + |
| 37 | +This problem can be solved using the greedy algorithm, since the car has to reach the destination using a minimum number |
| 38 | +of refueling stops. This means that the car needs a maximum fuel amount at any point. The idea is to make optimal choices |
| 39 | +by selecting the fuel station with the maximum fuel capacity at each step to reach the target distance with the minimum |
| 40 | +number of refueling stops. |
| 41 | + |
| 42 | +To cater to the problem of selecting the maximum fuel value, we can use the max-heap to keep track of fuel capacity for |
| 43 | +refueling because the top of the max-heap will always have the highest fuel value. Therefore we can take the highest fuel |
| 44 | +value from the top of the max-heap to reach the target by using the minimum number of refueling stops. To implement this |
| 45 | +methodology, we will create a function, min_refuel_stops. The steps of the function are given below: |
| 46 | + |
| 47 | +1. If the starting fuel is greater than or equal to the target distance, return 0. It means no extra fuel is required, |
| 48 | + and the car can reach the target using the starting fuel. |
| 49 | +2. Otherwise, iterate until the maximum distance is equal to or greater than the target, and the car is not out of fuel: |
| 50 | + - If we have a fuel station that can be used to refuel, and the vehicle has enough fuel to reach it, add the refueling |
| 51 | + station to the max-heap. |
| 52 | + - If the max-heap contains no fuel stations, the vehicle can’t reach the target, and the function returns −1. In |
| 53 | + simpler words, the car doesn’t have enough fuel to reach the target even after stopping at all the fuel stations. |
| 54 | + - Otherwise, if the max-heap has fuel stations and the vehicle doesn’t have enough fuel to go to the next station, |
| 55 | + the vehicle refuels from the fuel station with the maximum fuel value. After refueling, we remove the fuel value of |
| 56 | + this refueling station from the max-heap and also increment the number of stops. |
| 57 | +3. After executing the loop, the function returns the total number of refueling stops required to reach the target |
| 58 | + distance. |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +#### Time Complexity |
| 72 | + |
| 73 | +The time complexity of the solution above is O(n×log(n)), where n is the total number of stations. |
| 74 | + |
| 75 | +#### Space Complexity |
| 76 | + |
| 77 | +The space complexity of the solution above is O(n), since there will be maximum n fuel capacities in a heap. |
| 78 | + |
| 79 | + |
0 commit comments