853. Car Fleet
Asked at JPMorgan
Difficulty
Medium
Time
O(n log n)
Space
O(n)
Pattern
Array
Companies
1
Problem
There is a one-lane road that ends at mile target. You are given two arrays of length n: position[i] is the starting mile of car i, and speed[i] is how fast it drives (miles per hour). Every car drives toward target.
Cars cannot pass each other. If a faster car catches up to a slower car in front of it, it must slow down and drive right behind it. A fleet is one car alone, or a group of cars driving together. The speed of a fleet is the speed of its slowest car. If a car catches a fleet exactly at target, it still counts as part of that fleet.
Return the number of fleets that reach target.
Cars on a one-lane road
Constraints
- ●
n == position.length == speed.length - ●
1 <= n <= 10^5 - ●
0 < target <= 10^6 - ●
0 <= position[i] < target - ●
All values of position are unique - ●
0 < speed[i] <= 10^6
Examples
target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]3target = 10, position = [3], speed = [3]1target = 100, position = [0,2,4], speed = [4,2,1]1Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| JPMorgan | Medium | View all JPMorgan questions → |
How to Think About It
Cars cannot pass each other, so the only car that matters for a given car is the fleet directly ahead of it.
Compute each car's solo arrival time: (target - position) / speed.
Key insight: sort cars by position, closest to the target first. Walk backward: if a car's arrival time is less than or equal to the time of the fleet ahead, it catches that fleet and merges. Otherwise it arrives later and forms a new fleet, which becomes the new "fleet ahead".
You only need to remember the arrival time of the most recent fleet — a single variable (or a monotonic stack).
Walkthrough: target 12, positions [10,8,0,5,3], speeds [2,4,1,1,3]. Times by position desc: 10 -> 1, 8 -> 1, 5 -> 7, 3 -> 3, 0 -> 12. Fleets: 1 (new), 1 (merge), 7 (new), 3 (merge), 12 (new) -> 3 fleets.
Optimal Approach
Step 1: Pair cars as (position, speed) and sort by position descending.
Step 2: fleets = 0, lead = 0.
Step 3: For each car: t = (target - position) / speed. If t > lead: fleets += 1, lead = t.
Step 4: Return fleets.
Time: O(n log n) for sorting. Space: O(n).
Sort by position, then scan backward
What Trips People Up in Real Interviews
Simulating cars step by step. Arrival times make it a single sorted pass.
Sorting in the wrong direction. You must start from the car closest to the target.
Using >= for a new fleet. A car arriving at exactly the same time as the fleet ahead catches it at the target and joins it.
Integer division in C++/Java when computing times — use floating point.
Solution Code
O(n log n) timedef carFleet(target, position, speed):
cars = sorted(zip(position, speed), reverse=True)
fleets = 0
lead = 0.0
for p, s in cars:
t = (target - p) / s
if t > lead:
fleets += 1
lead = t
return fleetsFrequently Asked Questions
What is the Car Fleet problem?
Car Fleet puts cars on a one-lane road heading to a target. A faster car that catches a slower one ahead of it slows down and joins it as a fleet. The task is to count how many fleets arrive. The trick is processing cars from the one closest to the target backward and comparing arrival times.
How do you solve Car Fleet?
The optimal approach runs in O(n log n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Car Fleet?
Car Fleet is asked at JPMorgan. It is a medium difficulty problem.
What are common mistakes on Car Fleet?
- Simulating cars step by step. Arrival times make it a single sorted pass.
- Sorting in the wrong direction. You must start from the car closest to the target.
- Using `>=` for a new fleet. A car arriving at exactly the same time as the fleet ahead catches it at the target and joins it.
- Integer division in C++/Java when computing times — use floating point.