Car Fleet
Car Fleet is LeetCode 853 (Medium). n cars drive along a one-lane road toward mile target. Car i starts at position[i] and drives at speed[i]. A car can never pass the car in front: when it catches up, it slows down and the two drive on together as one fleet. A car that catches a fleet exactly at the target also joins it. Return how many fleets arrive.
- Up to 10⁵ cars, each starting at a different position before the target.
- Every speed is at least 1, so every car arrives.
- n == position.length == speed.length
- 1 <= n <= 10⁵
- 0 < target <= 10⁶
- 0 <= position[i] < target
- All the values of position are unique.
- 0 < speed[i] <= 10⁶
Intuition
Whether one car catches another depends on arrival times, not speeds. Driving alone, car i would reach the target at (target - position[i]) / speed[i].
Now walk from the car nearest the target back to the one furthest away. The nearest car has nothing in front of it, so it leads a fleet. A car behind a fleet that would arrive no later than that fleet must catch it on the way, and from then on it arrives with the fleet. A car that would arrive later never catches up, so it leads a new fleet, and its time becomes the one cars further back must beat.
So the answer is how many times the arrival time goes up as you walk back from the target.
Objects move in one line and cannot pass each other, so each one is only affected by what is in front of it. Sort by position, handle the front first, and keep just enough about the group ahead to decide each new object.
Approach
Before reading on, write the arrival time of each car for target = 12, position = [10, 8, 0, 5, 3], speed = [2, 4, 1, 1, 3]. Then read the times from the car nearest the target backwards and look for a pattern. Aim for O(n log n).
Sort nearest the target first
Pair each position with its speed and sort by position, largest first. A car is only ever held up by cars in front of it, so in this order the cars that could block the current one have all been seen.
Turn each car into an arrival time
Compute (target - pos) / spd as a float. The car behind catches the one in front before or at the target exactly when its own time is no larger, so comparing two numbers replaces any meeting-point algebra.
Compare with the fleet ahead
Keep lead_time, the arrival time of the nearest fleet in front. If the current car's time is larger, it never catches that fleet: count a new fleet and store its time. Otherwise it joins, and lead_time stays, since the car now arrives with that fleet.
Why one number is enough
Each new fleet arrives later than every fleet in front of it, so lead_time is also the largest time seen so far. A car that cannot catch the nearest fleet is stuck behind it and can never reach the fleets further ahead, so nothing else needs to be stored.
Code in Python, C++ and Java
Common pitfalls
Walking from the back of the road
cars = sorted(zip(position, speed))
cars = sorted(zip(position, speed), reverse=True)
From the back, the car being judged has not yet met the cars that block it, so the running time means nothing. On target = 12, position = [10, 8, 0, 5, 3], speed = [2, 4, 1, 1, 3] this returns 1 instead of 3.
Splitting cars that arrive together
if time >= lead_time:
if time > lead_time:
A car whose time equals the fleet's catches it exactly at the target, and that still counts as one fleet. On target = 10, position = [6, 8], speed = [2, 1] both times are 2, and >= returns 2 instead of 1.
Integer division in C++ or Java
double time = (target - pos) / spd;
double time = (double)(target - pos) / spd;
Both operands are int, so the division drops the fraction before the result becomes a double. On target = 10, position = [0, 1], speed = [3, 3] the times 3.33 and 3 both become 3, the cars merge, and the answer 2 turns into 1.
Edge cases
Its time is positive because it starts before the target, so it beats the starting lead_time of 0 and the answer is 1.
With target = 10, a car at 0 with speed 5 arrives at 2 and a car at 9 with speed 1 arrives at 1. The fast car is still 2 behind on arrival time, so it never catches up and the answer is 2.
Complexity
Car Fleet FAQ
Do I need a stack?
No. The common stack version pushes each car's time and pops it again when that car joins the fleet in front, so the stack ends up holding one time per fleet. Only its top is ever compared, and one variable holding that top does the same job.
Are floating-point arrival times safe here?
Yes. Two cars tie only when their two fractions are equal, and equal fractions divide to the same double. Two different fractions with these limits differ by at least about one part in 10¹², while a double keeps about 16 digits. For exact integer maths, compare (target - p1) * s2 with (target - p2) * s1 in 64-bit integers instead.