Car Fleet
Difficulty: Medium
Cars drive in one lane toward the same destination, and no car can pass the car in front. You're given each car's starting position and speed.
If a car catches up to the car ahead before the destination, it slows to match that car's speed, and they travel on as one "fleet". Given the destination, return how many separate fleets arrive.
Examples
Input: target = 12, position = [10, 8, 0, 5, 3], speed = [2, 4, 1, 1, 3]
Output: 3
The car at 10 reaches the destination on its own. The car at 8 is fast enough to catch up to it first, and they arrive together as one fleet. The car at 5 travels alone at first, but the car at 3 (which is faster) catches up to it, forming a second fleet. The car at 0 never catches anyone, forming a third fleet by itself. That's 3 fleets in total.
Input: target = 10, position = [3], speed = [3]
Output: 1
With only one car on the road, it's automatically its own fleet.
Input: target = 100, position = [0, 2, 4], speed = [4, 2, 1]
Output: 1
The car at 0 is the fastest and catches up to the car at 2, and that combined group in turn catches up to the slowest car, at 4. Everyone ends up traveling together as a single fleet.
Constraints
n == position.length == speed.length
1 <= n <= 10^5
0 < target <= 10^6
0 <= position[i] < target
Every value in position is unique.
0 < speed[i] <= 10^6
Approach
Cars can't pass, so each car either arrives alone or catches the car (or group) ahead and joins that fleet. This depends only on the car directly ahead: if a car can't catch that one, it can't catch anything further ahead either.
So:
- Compute each car's solo arrival time: how long it would take driving alone.
- Process cars from closest to the destination to farthest, tracking the arrival time of the latest fleet.
- If a car would arrive at or before that time, it catches up and merges into that fleet.
- If it would arrive later, it can never catch up, so it leads a new fleet.
The number of fleets formed is the answer.
Solutions
Brute Force — Repeatedly Merge Adjacent Cars
Sort cars by position. Scan and merge any adjacent pair where the rear car would arrive at or before the car ahead (it caught up). The merged group behaves like the front car. Repeat until a full pass makes no merges.
function carFleet(target, position, speed) {
let groups = position.map((p, i) => ({ pos: p, time: (target - p) / speed[i] }));
groups.sort((a, b) => a.pos - b.pos); // farthest from target (rear-most) first
let merged = true;
while (merged) {
merged = false;
const next = [];
let i = 0;
while (i < groups.length) {
if (i + 1 < groups.length && groups[i].time <= groups[i + 1].time) {
// the rear group catches the group ahead of it; they become one fleet
next.push({ pos: groups[i + 1].pos, time: groups[i + 1].time });
i += 2;
merged = true;
} else {
next.push(groups[i]);
i += 1;
}
}
groups = next;
}
return groups.length;
}Time: O(n²) in the worst case, on top of the initial O(n log n) sort · Space: O(n)
Optimal — Sort + Monotonic Stack
Compute each car's solo arrival time. Process cars from closest to the destination to farthest, keeping a stack of fleet arrival times. A car merges into the fleet ahead if its time is less than or equal to the top of the stack; otherwise it leads a new fleet.
function carFleet(target, position, speed) {
const cars = position
.map((p, i) => [p, speed[i]])
.sort((a, b) => b[0] - a[0]); // closest to target first
const stack = [];
for (const [pos, spd] of cars) {
const time = (target - pos) / spd;
if (stack.length === 0 || time > stack[stack.length - 1]) {
stack.push(time);
}
// otherwise this car catches up to the fleet ahead before the target,
// so it merges instead of forming a new fleet
}
return stack.length;
}Time: O(n log n), dominated by the sort · Space: O(n)