## 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.

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.

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.

When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.

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`

.

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:0Explanation:We can reach the target without refueling.

**Example 2:**

Input:target = 100, startFuel = 1, stations = [[10,100]]Output:-1Explanation:We can't 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:2Explanation: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.

**Note:**

`1 <= target, startFuel, stations[i][1] <= 10^9`

`0 <= stations.length <= 500`

`0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target`

## Rust Solution

```
struct Solution;
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::collections::HashMap;
impl Solution {
fn min_refuel_stops(target: i32, start_fuel: i32, mut stations: Vec<Vec<i32>>) -> i32 {
let mut queue: BinaryHeap<(Reverse<usize>, usize, i32)> = BinaryHeap::new();
stations.push(vec![target, 0]);
let n = stations.len();
let fuel = start_fuel - stations[0][0];
let mut states: HashMap<(usize, usize), i32> = HashMap::new();
if fuel >= 0 {
states.insert((0, 0), fuel);
queue.push((Reverse(0), 0, fuel));
}
while let Some((Reverse(stop), i, fuel)) = queue.pop() {
if i == n - 1 {
return stop as i32;
}
let dist = stations[i + 1][0] - stations[i][0];
let take = fuel + stations[i][1] - dist;
let skip = fuel - dist;
for &(s, f) in &[(stop + 1, take), (stop, skip)] {
if f < 0 {
continue;
}
if let Some(&max_f) = states.get(&(i + 1, s)) {
if max_f >= f {
continue;
}
}
*states.entry((i + 1, s)).or_default() = f;
queue.push((Reverse(s), i + 1, f));
}
}
-1
}
}
#[test]
fn test() {
let target = 1;
let start_fuel = 1;
let stations = vec_vec_i32![];
let res = 0;
assert_eq!(
Solution::min_refuel_stops(target, start_fuel, stations),
res
);
let target = 100;
let start_fuel = 1;
let stations = vec_vec_i32![[10, 100]];
let res = -1;
assert_eq!(
Solution::min_refuel_stops(target, start_fuel, stations),
res
);
let target = 100;
let start_fuel = 10;
let stations = vec_vec_i32![[10, 60], [20, 30], [30, 30], [60, 40]];
let res = 2;
assert_eq!(
Solution::min_refuel_stops(target, start_fuel, stations),
res
);
}
```

Having problems with this solution? Click here to submit an issue on github.