Skip to content

Commit

Permalink
Add problem 1870: Minimum Speed to Arrive on Time
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Apr 30, 2024
1 parent 51296a2 commit b780f2b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,7 @@ pub mod problem_1864_minimum_number_of_swaps_to_make_the_binary_string_alternati
pub mod problem_1865_finding_pairs_with_a_certain_sum;
pub mod problem_1866_number_of_ways_to_rearrange_sticks_with_k_sticks_visible;
pub mod problem_1869_longer_contiguous_segments_of_ones_than_zeros;
pub mod problem_1870_minimum_speed_to_arrive_on_time;
pub mod problem_1876_substrings_of_size_three_with_distinct_characters;
pub mod problem_1877_minimize_maximum_pair_sum_in_array;
pub mod problem_1880_check_if_word_equals_summation_of_two_words;
Expand Down
59 changes: 59 additions & 0 deletions src/problem_1870_minimum_speed_to_arrive_on_time/binary_search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

use std::num::{NonZeroU32, NonZeroU64};

impl Solution {
fn check(dist: &[i32], last_dist: i32, hour: u64, speed: NonZeroU32) -> bool {
let speed = NonZeroU64::from(speed);
let mut cost = 0;

for &dist in dist {
cost += (u64::from(dist as u32) + (speed.get() - 1)) / speed;
}

(cost * speed.get() + u64::from(last_dist as u32)) * 100 > hour * speed.get()
}

pub fn min_speed_on_time(dist: Vec<i32>, hour: f64) -> i32 {
let (&last_dist, dist) = dist.split_last().unwrap();
let hour = (hour * 100.0).round() as u64;
let mut start = 1;
let mut count = u32::MAX - 1;

loop {
let half = count / 2;
let middle = start + half;

if Self::check(dist, last_dist, hour, NonZeroU32::new(middle).unwrap()) {
start = middle + 1;
count -= half + 1;
} else {
count = half;
}

if count == 0 {
break;
}
}

start as _
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn min_speed_on_time(dist: Vec<i32>, hour: f64) -> i32 {
Self::min_speed_on_time(dist, hour)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
22 changes: 22 additions & 0 deletions src/problem_1870_minimum_speed_to_arrive_on_time/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub mod binary_search;

pub trait Solution {
fn min_speed_on_time(dist: Vec<i32>, hour: f64) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
((&[1, 3, 2] as &[_], 6.0), 1),
((&[1, 3, 2], 2.7), 3),
((&[1, 3, 2], 1.9), -1),
];

for ((dist, hour), expected) in test_cases {
assert_eq!(S::min_speed_on_time(dist.to_vec(), hour), expected);
}
}
}

0 comments on commit b780f2b

Please sign in to comment.