A number of cars are on a one-lane road, all heading to the same mile marker, target. Car i starts at mile position[i] and drives at speed[i] miles per hour. No two cars start at the same spot.
Cars cannot overtake. When a faster car reaches a slower one, it stays right behind it and drives at the slower speed from then on. Cars driving together like this form a fleet; a lone car is a fleet too. A car that catches up exactly at the target joins that fleet.
Return how many fleets reach the target.
Example 1
Input: target = 10, position = [8, 3, 5, 0], speed = [2, 1, 5, 2] Output: 2 Why: 8 and 5 both arrive at hour 1; 0 catches the slow car at 3
2 more examples and the constraints
Example 2
Input: target = 7, position = [2], speed = [4] Output: 1 Why: a single car is a fleet
Example 3
Input: target = 20, position = [0, 5, 10], speed = [1, 2, 3] Output: 3 Why: each car is slower than the one ahead, so nobody catches up
Constraints
- 1 ≤ number of cars ≤ 100,000
- 0 < target ≤ 1,000,000
- 0 ≤ position[i] < target, and all positions are different
- 0 < speed[i] ≤ 1,000,000
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Check every car aheadO(n²)
- Sort, then stack the fleetsO(n log n)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 100,000 cars | Verdict |
|---|---|---|---|
| Check every car ahead | O(n²) | Mention it, then move on | |
| Sort, then stack the fleets | O(n log n) | Best; one sort and one pass |
n = number of cars.
Edge cases to test
- One car. Example 2: the answer is 1.
- Catching up exactly at the target. Two cars that arrive at the same moment are one fleet. Use
>for a new fleet, not>=. - Nobody catches anyone. Example 3: each car is slower than the one ahead, so every car is its own fleet.
- A fast car far behind. It may catch a fleet that itself caught a slower car; it still joins just one fleet.
- Times that are not whole numbers. 7 / 3 and 9 / 4 are close. Compare with products of integers instead of dividing.
Common mistakes
- Reading the cars from the back instead of from the front, so a car is compared with the wrong fleet.
- Counting a car that ties the fleet ahead as a new fleet.
- Comparing a car with the car directly ahead instead of with that car's fleet, which may be slower.
How to talk through it
- 1Clarify. Can positions repeat? Does catching up exactly at the target count as one fleet?
- 2Give the brute force. A car leads a fleet if no car ahead is at least as slow to arrive; checking all pairs is O(n²).
- 3Sort by position. Only the cars in front matter, so read them from the front and track arrival times.
- 4Code the stack. Push a car only when it needs more time than the fleet on top; the stack size is the answer.
- 5Test out loud. Run the single car, the tie at the target and the case where nobody catches up.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?