20241007 finished.
This commit is contained in:
parent
87428f2b3b
commit
d1610560ff
|
@ -256,3 +256,4 @@ mod p1870_minimum_speed_to_arrive_on_time;
|
||||||
mod p1928_minimum_cost_to_reach_destination_in_time;
|
mod p1928_minimum_cost_to_reach_destination_in_time;
|
||||||
mod p1227_airplane_seat_assignment_probability;
|
mod p1227_airplane_seat_assignment_probability;
|
||||||
mod p2187_minimum_time_to_complete_trips;
|
mod p2187_minimum_time_to_complete_trips;
|
||||||
|
mod p871_minimum_number_of_refueling_stops;
|
55
src/problem/p871_minimum_number_of_refueling_stops.rs
Normal file
55
src/problem/p871_minimum_number_of_refueling_stops.rs
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
/**
|
||||||
|
* [871] Minimum Number of Refueling Stops
|
||||||
|
*/
|
||||||
|
pub struct Solution {}
|
||||||
|
|
||||||
|
|
||||||
|
// submission codes start here
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn min_refuel_stops(target: i32, start_fuel: i32, stations: Vec<Vec<i32>>) -> i32 {
|
||||||
|
use std::collections::BinaryHeap;
|
||||||
|
let mut stations: Vec<(i32, i32)> = stations.iter().map(|v| (v[0], v[1])).collect();
|
||||||
|
stations.push((target, 0));
|
||||||
|
|
||||||
|
let mut heap = BinaryHeap::new();
|
||||||
|
let mut fuel = start_fuel;
|
||||||
|
let mut last_pos = 0;
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
for (p, f) in stations {
|
||||||
|
while fuel < p - last_pos {
|
||||||
|
if let Some(head) = heap.pop() {
|
||||||
|
fuel += head;
|
||||||
|
count += 1;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fuel -= p - last_pos;
|
||||||
|
last_pos = p;
|
||||||
|
heap.push(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
if fuel >= 0 {
|
||||||
|
count
|
||||||
|
} else {
|
||||||
|
-1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// submission codes end
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_871() {
|
||||||
|
assert_eq!(0, Solution::min_refuel_stops(1, 1, vec![]));
|
||||||
|
assert_eq!(-1, Solution::min_refuel_stops(100, 1, vec![vec![10, 100]]));
|
||||||
|
assert_eq!(2, Solution::min_refuel_stops(100, 10, vec![vec![10, 60], vec![20, 30], vec![30, 30], vec![60, 40]]));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user