20241005 finished.

This commit is contained in:
jackfiled 2024-10-05 16:59:00 +08:00
parent f02363d377
commit 87428f2b3b
2 changed files with 54 additions and 1 deletions

View File

@ -255,3 +255,4 @@ mod p983_minimum_cost_for_tickets;
mod p1870_minimum_speed_to_arrive_on_time; 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;

View File

@ -0,0 +1,52 @@
/**
* [2187] Minimum Time to Complete Trips
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn minimum_time(time: Vec<i32>, total_trips: i32) -> i64 {
let time: Vec<i64> = time.iter().map(|x| *x as i64).collect();
let total_trips = total_trips as i64;
let check = |t: i64| -> bool {
let mut trips = 0;
for &bus in time.iter() {
trips += t / bus;
}
trips >= total_trips
};
let mut left = 0;
let mut right = time[0] * total_trips;
while left < right {
let middle = (right - left) / 2 + left;
if check(middle) {
right = middle;
} else {
left = middle + 1;
}
}
right
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2187() {
assert_eq!(3, Solution::minimum_time(vec![1,2,3], 5));
assert_eq!(2, Solution::minimum_time(vec![2], 1));
}
}