Example 1
ready- Input
target = 12 position = [ 10, 8, 0, 5, 3 ] speed = [ 2, 4, 1, 1, 3 ]- Expected
3- Why
- the cars form fleets led from positions 10, 5, and 0
The fastest car doesn't always arrive alone. If traffic ahead is slower, speed turns into waiting.
You're given a destination coordinate target and two matching arrays. Car i starts
at position[i] and moves toward the destination at speed[i]. Every starting position
is different and lies before target.
When a car catches a slower car or group ahead, they become a fleet — one unit that
continues at the speed of its slowest member. Cars never pass. Return how many fleets
reach target.
A few ground rules:
target still produces one fleet.d1 / v1 and
d2 / v2 by cross-multiplying d1 * v2 and d2 * v1. Ordinary real division also
has a safe margin on the generated inputs.1 <= position.length == speed.length <= 100_0000 <= position[i] < target <= 1_000_000_000position are distinct.1 <= speed[i] <= 1_000_000Hints
A car's solo arrival time is (target - position[i]) / speed[i]. Which car must you
examine first if you want to know whether the next one catches it?
Sort by position from closest to target to farthest. A car behind joins the fleet
ahead when its solo arrival time is less than or equal to that fleet's arrival time.
As you scan backward along the road, record an arrival time only when it is strictly greater than the last recorded time. Those recorded times increase, and each one represents a fleet the cars ahead can't absorb.
Can you prove that comparing a car only with the latest fleet time is enough, even after that fleet has absorbed several faster cars?
Visible cases
target = 12
position = [
10,
8,
0,
5,
3
]
speed = [
2,
4,
1,
1,
3
]3target = 10
position = [
6,
8
]
speed = [
3,
2
]2target = 10
position = [
0,
4,
8
]
speed = [
4,
2,
1
]2Interview signal
Fleet decisions must happen from the destination backward. Without sorting, you can repeatedly search every unused car for the greatest position, then process that car's solo arrival time.
def car_fleet(target: int, position: list[int], speed: list[int]) -> int:
used = [False] * len(position)
fleet_times: list[float] = []
for _ in position:
front = -1
for i in range(len(position)):
if not used[i] and (front == -1 or position[i] > position[front]):
front = i
used[front] = True
arrival = (target - position[front]) / speed[front]
if not fleet_times or arrival > fleet_times[-1]:
fleet_times.append(arrival)
return len(fleet_times)If a car's solo arrival is no later than the latest fleet's arrival, it catches that fleet before or at the destination. A later arrival means it remains behind as a new fleet.
Time O(n²) — finding the next frontmost car scans the remaining input each time. Space O(n) for the used markers and fleet times.
Pay for the road order once. Pair every position with its arrival time, sort positions from greatest to smallest, then make the same fleet decision in one pass.
The arrival times you keep form a monotonic stack — each stored time is strictly greater than the one before it. A time less than or equal to the top disappears into that existing fleet; a greater time starts a fleet that can't catch anything ahead.
def car_fleet(target: int, position: list[int], speed: list[int]) -> int:
cars = sorted(zip(position, speed), reverse=True)
fleet_times: list[float] = []
for start, velocity in cars:
arrival = (target - start) / velocity
if not fleet_times or arrival > fleet_times[-1]:
fleet_times.append(arrival)
return len(fleet_times)The fractional comparison has an exact form too. For distances d1, d2 and positive
speeds v1, v2, compare d1 * v2 with d2 * v1. The largest possible product here
is 10^15, which fits a signed 64-bit integer and JavaScript's exact integer range. The
test data also keeps unequal floating arrival times at least 10^-9 apart.
Time O(n log n) for sorting, followed by an O(n) scan. Space O(n) for the sorted cars and the time stack.
Ready to run.
3 cases are queued.
Visible testcase
Case 1
Expected
3