-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1870: Minimum Speed to Arrive on Time
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/problem_1870_minimum_speed_to_arrive_on_time/binary_search.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |