# 871. Minimum Number of Refueling Stops¶

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.

**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.
```

**Constraints:**

`1 <= target, startFuel <= 109`

`0 <= stations.length <= 500`

`0 <= positioni <= positioni+1 < target`

`1 <= fueli < 109`

## Analysis¶

This problem can be solved using Dynamic Programming and Greedy algorithm.

### Dynamic Programming¶

We could create a `dp[i]`

array represent how much fuel can be added at `i`

th station. To check if target miles can be reached at a given station, we just need to compare `dp[i]`

and target miles, because that means at `i`

th station we can go to `dp[i]`

miles from our `dp[i]`

fuel.

To update our `dp[i]`

, we can traverse all the stations and update each `dp[i]`

with the maximum result from all the previous populated `dp[j]`

where `j < i`

.

- Time: O(n^2)
- Space: O(n)

### Greedy¶

We don't necessarily need to compare all the stations before our current station. Instead, we just need to find the stations that hold the maximum of fuel in front of us. It will save us from O(n^2) to O(n \times \log(n))

- Time: O(n \times \log(n))
- Space: O(n)

## Code (DP)¶

```
class Solution {
public:
int minRefuelStops(int target, int startFuel/* 1 mile 1 fuel, so it means intial target miles on the east can reach */,
vector<vector<int>>& stations/* stations is sorted by position */) {
vector<long long> dp(stations.size() + 1); //dp[i]: store fuel left at ith gas station
dp[0] = startFuel;
for (int i = 0; i < stations.size(); ++i) {
for (int j = i; ~j; --j) {
if (dp[j] >= stations[i][0]) // can be fueled
dp[j + 1] = max(dp[j + 1], dp[j] + stations[i][1]);
}
}
for (int i = 0; i < dp.size(); ++i)
if (dp[i] >= target) return i;
return -1;
}
};
```

## Code (Greedy with Heap)¶

```
class Solution {
public:
int minRefuelStops(int target, int startFuel/* 1 mile 1 fuel, so it means intial target miles on the east can reach */,
vector<vector<int>>& stations/* stations is sorted by position */) {
// at target position, can add 0 fuel
stations.push_back({target, 0});
int res = 0, currFuel = startFuel;
// store the current max fuel
priority_queue<int> q;
for (auto& s : stations) {
int pos = s[0], fuel = s[1];
// keep adding fuel until reachable miles is greater or equal to current position
while (q.size() && currFuel < pos) {
currFuel += q.top();
q.pop();
res ++;
}
// cannot reach even using the the maximum fuel
if (currFuel < pos) return -1;
q.push(fuel);
}
return res;
}
};
```