LeetCode 853. Car Fleet

Problem Statement


impl Solution {
pub fn car_fleet(target: i32, position: Vec<i32>, speed: Vec<i32>) -> i32 {
let n = position.len();
if n == 0 || n == 1 {
return n as i32;
}
let mut src = vec![(0, 0.); n];
for i in 0..n {
src[i].0 = position[i];
src[i].1 = (target - position[i]) as f64 / speed[i] as f64;
}

src.sort_by_key(|x| x.0);
let mut ans = 0;
for i in (1..n).rev() {
if src[i].1 < src[i - 1].1 {
ans += 1; continue;
}
src[i - 1] = src[i];
}
return ans + 1;
}
}