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:

  • positioni is the distance between the ith car and the beginning of the road in meters. It is guaranteed that positioni < positioni+1.
  • speedi is the initial speed of the ith car in meters per second.

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.

Return an array answer, where answer[i] is the time, in seconds, at which the ith 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.

Input & Output

Example 1 — Basic Case
$ Input: cars = [[1,2],[3,1],[5,3]]
Output: [2.0,-1,-1]
💡 Note: Car 0 (pos=1, speed=2) catches Car 1 (pos=3, speed=1) at time (3-1)/(2-1) = 2.0. Car 1 cannot catch Car 2 (slower speed). Car 2 is last.
Example 2 — No Collisions
$ Input: cars = [[1,1],[2,2],[3,3]]
Output: [-1,-1,-1]
💡 Note: Each car is slower than the one ahead, so no collisions occur. All return -1.
Example 3 — Multiple Collisions
$ Input: cars = [[1,4],[2,3],[4,1]]
Output: [1.0,1.0,-1]
💡 Note: Car 0 catches Car 1 at time (2-1)/(4-3) = 1.0. Car 1 catches Car 2 at time (4-2)/(3-1) = 1.0. Car 2 is last.

Constraints

  • 1 ≤ cars.length ≤ 105
  • 1 ≤ positioni, speedi ≤ 106
  • positioni < positioni+1

Visualization

Tap to expand
Car Fleet II - Collision Time INPUT 0 1 2 pos:1 pos:3 pos:5 cars array: Car 0 [1, 2] Car 1 [3, 1] Car 2 [5, 3] Speeds (m/s): 2 m/s 1 m/s 3 m/s Direction --> Speed Analysis: Car 0 (2) > Car 1 (1) Will catch up! ALGORITHM STEPS 1 Process Right to Left Use stack, start from last car 2 Check Collision time = (pos2-pos1)/(spd1-spd2) 3 Stack Maintenance Pop if no collision possible 4 Record Result Store collision time or -1 Calculation Example: Car 2: Last car, no next answer[2] = -1 Car 1: speed 1 < speed 3 answer[1] = -1 (slower) Car 0: speed 2 > speed 1 t = (3-1)/(2-1) = 2.0 answer[0] = 2.0 FINAL RESULT Collision Timeline t=0 t=2s t=inf 0 hits 1 Output Array: 2.0 -1 -1 [0] [1] [2] Explanation: Car 0: Collides with Car 1 at t = 2.0 seconds Car 1: Slower than Car 2 Never catches up = -1 Car 2: Last car, no next Key Insight: Process cars from RIGHT to LEFT using a monotonic stack. A faster car will catch a slower car ahead. Collision time formula: t = (position_next - position_current) / (speed_current - speed_next) Stack helps track which car to collide with, considering chain collisions. Time: O(n), Space: O(n) TutorialsPoint - Car Fleet II | Optimal Solution (Monotonic Stack)
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen