|
| 1 | +/** |
| 2 | + * [1776] Car Fleet II |
| 3 | + * |
| 4 | + * There are n cars traveling at different speeds in the same direction along a one-lane road. You are given an array cars of length n, where cars[i] = [positioni, speedi] represents: |
| 5 | + * |
| 6 | + * positioni is the distance between the i^th car and the beginning of the road in meters. It is guaranteed that positioni < positioni+1. |
| 7 | + * speedi is the initial speed of the i^th car in meters per second. |
| 8 | + * |
| 9 | + * For simplicity, cars can be considered as points moving along the number line. Two cars collide when they occupy the same position. Once a car collides with another car, they unite and form a single car fleet. The cars in the formed fleet will have the same position and the same speed, which is the initial speed of the slowest car in the fleet. |
| 10 | + * Return an array answer, where answer[i] is the time, in seconds, at which the i^th car collides with the next car, or -1 if the car does not collide with the next car. Answers within 10^-5 of the actual answers are accepted. |
| 11 | + * |
| 12 | + * Example 1: |
| 13 | + * |
| 14 | + * Input: cars = [[1,2],[2,1],[4,3],[7,2]] |
| 15 | + * Output: [1.00000,-1.00000,3.00000,-1.00000] |
| 16 | + * Explanation: After exactly one second, the first car will collide with the second car, and form a car fleet with speed 1 m/s. After exactly 3 seconds, the third car will collide with the fourth car, and form a car fleet with speed 2 m/s. |
| 17 | + * |
| 18 | + * Example 2: |
| 19 | + * |
| 20 | + * Input: cars = [[3,4],[5,4],[6,3],[9,1]] |
| 21 | + * Output: [2.00000,1.00000,1.50000,-1.00000] |
| 22 | + * |
| 23 | + * |
| 24 | + * Constraints: |
| 25 | + * |
| 26 | + * 1 <= cars.length <= 10^5 |
| 27 | + * 1 <= positioni, speedi <= 10^6 |
| 28 | + * positioni < positioni+1 |
| 29 | + * |
| 30 | + */ |
| 31 | +pub struct Solution {} |
| 32 | + |
| 33 | +// problem: https://leetcode.com/problems/car-fleet-ii/ |
| 34 | +// discuss: https://leetcode.com/problems/car-fleet-ii/discuss/?currentPage=1&orderBy=most_votes&query= |
| 35 | + |
| 36 | +// submission codes start here |
| 37 | + |
| 38 | +impl Solution { |
| 39 | + // Credit: https://leetcode.com/problems/car-fleet-ii/solutions/3213685/just-a-runnable-solution/ |
| 40 | + pub fn get_collision_times(cars: Vec<Vec<i32>>) -> Vec<f64> { |
| 41 | + let mut stack: Vec<usize> = Vec::new(); |
| 42 | + let mut result = vec![-1.0; cars.len()]; |
| 43 | + |
| 44 | + for i in (0..cars.len()).rev() { |
| 45 | + while !stack.is_empty() |
| 46 | + && (cars[i][1] <= cars[stack[stack.len() - 1]][1] |
| 47 | + || (stack.len() > 1 |
| 48 | + && Self::collision_time(&cars, i, stack[stack.len() - 1]) |
| 49 | + >= result[stack[stack.len() - 1]])) |
| 50 | + { |
| 51 | + stack.pop(); |
| 52 | + } |
| 53 | + let time = if stack.is_empty() { |
| 54 | + -1.0 |
| 55 | + } else { |
| 56 | + Self::collision_time(&cars, i, stack[stack.len() - 1]) |
| 57 | + }; |
| 58 | + result[i] = time; |
| 59 | + stack.push(i); |
| 60 | + } |
| 61 | + |
| 62 | + result |
| 63 | + } |
| 64 | + |
| 65 | + pub fn collision_time(cars: &[Vec<i32>], curr: usize, next: usize) -> f64 { |
| 66 | + (cars[next][0] - cars[curr][0]) as f64 / (cars[curr][1] - cars[next][1]) as f64 |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// submission codes end |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + use super::*; |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_1776_example_1() { |
| 78 | + let cars = vec![vec![1, 2], vec![2, 1], vec![4, 3], vec![7, 2]]; |
| 79 | + |
| 80 | + let result = vec![1.00000, -1.00000, 3.00000, -1.00000]; |
| 81 | + |
| 82 | + assert_eq!(Solution::get_collision_times(cars), result); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_1776_example_2() { |
| 87 | + let cars = vec![vec![3, 4], vec![5, 4], vec![6, 3], vec![9, 1]]; |
| 88 | + |
| 89 | + let result = vec![2.00000, 1.00000, 1.50000, -1.00000]; |
| 90 | + |
| 91 | + assert_eq!(Solution::get_collision_times(cars), result); |
| 92 | + } |
| 93 | +} |
0 commit comments