-
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.
- Loading branch information
Showing
3 changed files
with
83 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
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,24 @@ | ||
pub mod monotonic_stack; | ||
|
||
pub trait Solution { | ||
fn get_collision_times(cars: Vec<Vec<i32>>) -> Vec<f64>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
&[[1, 2], [2, 1], [4, 3], [7, 2]] as &[_], | ||
&[1.0, -1.0, 3.0, -1.0] as &[_], | ||
), | ||
(&[[3, 4], [5, 4], [6, 3], [9, 1]], &[2.0, 1.0, 1.5, -1.0]), | ||
]; | ||
|
||
for (cars, expected) in test_cases { | ||
assert_eq!(S::get_collision_times(cars.iter().map(Vec::from).collect()), expected); | ||
} | ||
} | ||
} |
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,58 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
// See <https://leetcode.com/problems/car-fleet-ii/discuss/1085987/JavaC%2B%2BPython-O(n)-Stack-Solution>. | ||
|
||
use std::convert::TryInto; | ||
|
||
impl Solution { | ||
pub fn get_collision_times(cars: Vec<Vec<i32>>) -> Vec<f64> { | ||
let mut result = vec![-1.0; cars.len()]; | ||
let mut stack = Vec::<(u32, u32, u32, u32)>::new(); | ||
|
||
for (target, car) in result.iter_mut().zip(cars).rev() { | ||
let [position, speed]: [_; 2] = car.try_into().ok().unwrap(); | ||
let (position, speed) = (position as u32, speed as u32); | ||
|
||
let (distance, speed_diff) = loop { | ||
if let Some(&(right_position, right_speed, numerator, denominator)) = stack.last() { | ||
if speed > right_speed { | ||
let distance = right_position - position; | ||
let speed_diff = speed - right_speed; | ||
|
||
if u64::from(distance) * u64::from(denominator) < u64::from(numerator) * u64::from(speed_diff) { | ||
*target = f64::from(distance) / f64::from(speed_diff); | ||
|
||
break (distance, speed_diff); | ||
} | ||
} | ||
|
||
stack.pop(); | ||
} else { | ||
break (u32::MAX, 0); | ||
} | ||
}; | ||
|
||
stack.push((position, speed, distance, speed_diff)); | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn get_collision_times(cars: Vec<Vec<i32>>) -> Vec<f64> { | ||
Self::get_collision_times(cars) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |