Skip to content

Commit 9fffb11

Browse files
committed
feat: resolved but not understand
1 parent 960c11a commit 9fffb11

File tree

1 file changed

+19
-3
lines changed
  • minumum_number_of_refueling_stops/src

1 file changed

+19
-3
lines changed

minumum_number_of_refueling_stops/src/lib.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
pub struct Solution {}
22

33
impl Solution {
4-
pub fn min_refuel_stops(target: i32, start_fuel: i32, stations: Vec<Vec<i32>>) -> i32 {}
4+
pub fn min_refuel_stops(target: i32, start_fuel: i32, stations: Vec<Vec<i32>>) -> i32 {
5+
let mut furthest = vec![0; stations.len() + 1]; // furthest distance after ith refueling
6+
furthest[0] = start_fuel;
7+
for i in 0..stations.len() {
8+
for t in 0..i + 1 {
9+
if furthest[t] >= stations[i][0] {
10+
furthest[t + 1] = furthest[t + 1].max(furthest[t] + stations[i][1]);
11+
}
12+
}
13+
}
14+
for (t, d) in furthest.iter().enumerate() {
15+
if *d >= target {
16+
return t as i32;
17+
}
18+
}
19+
-1
20+
}
521
}
622

723
#[cfg(test)]
@@ -21,7 +37,7 @@ mod tests {
2137
stations
2238
.to_vec()
2339
.iter()
24-
.map(|x| x.to_vec())
40+
.map(|x: &[i32; 2]| x.to_vec())
2541
.collect::<Vec<Vec<i32>>>()
2642
),
2743
expected
@@ -53,7 +69,7 @@ mod tests {
5369
let target = 100;
5470
let start_fuel = 10;
5571
let stations = [[10, 60], [20, 30], [30, 30], [60, 40]];
56-
let expected = 0;
72+
let expected = 2;
5773
assert_eq!(
5874
Solution::min_refuel_stops(
5975
target,

0 commit comments

Comments
 (0)