Skip to content
AI360Xpert
Beta
Difficulty: MediumStack

Car Fleet

Problem in Plain English

Given a target mile marker plus each car starting position and speed, where faster cars queue behind slower ones instead of passing, return how many distinct fleets arrive at the target.

Problem Statement

There are n cars going to the same destination along a one-lane road. You are given the integer target (the destination position), an array position of starting positions, and an array speed of speeds.

A car can never pass the car ahead of it: a faster car catches up and then follows at the slower speed, and cars arriving together count as one fleet. Return the number of car fleets that arrive at the destination. For target = 12 with position = [10,8,0,5,3] and speed = [2,4,1,1,3] the answer is 3.

Constraints

  • n == position.length == speed.length
  • 1 <= n <= 10^5
  • 0 < target <= 10^6
  • 0 <= position[i] < target
  • All the values of position are unique.
  • 0 < speed[i] <= 10^6

Examples

Example 1

Input
target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
Output
3

Positions 10 and 8 meet at target 12 and arrive at time 1. Positions 5 and 3 meet at mile 6; their fleet then travels at speed 1 and arrives at time 7. Position 0 travels alone and arrives at time 12. These three groups give 3 fleets.

Example 2

Input
target = 10, position = [3], speed = [3]
Output
1

A single car always forms exactly one fleet.

Example 3

Input
target = 100, position = [0,2,4], speed = [4,2,1]
Output
1

The rear cars at 0 and 2 first merge at mile 4 after 1 hour. That fleet slows to speed 2, catches the original front car at mile 6 after 2 hours, then slows to speed 1. All three arrive together at time 96, giving 1 fleet.

Example 4

Input
target = 10, position = [8,6], speed = [1,2]
Output
1

Both solo times are 2. The rear car catches the front car exactly at the destination, and a meeting at the target still counts as one fleet.

Intuition

Cars cannot pass, so traffic order is fixed by starting position. Replace motion with solo arrival time (target - position) / speed. A rear car arriving no later than the fleet immediately ahead catches it by the target and adopts its arrival time. In Example 1 the nearest-first times are [1,1,7,3,12]: 1 joins 1, 3 joins 7, and 12 stays separate. The bottleneck is the nearest existing fleet, whose arrival time is the largest processed time.

Approaches

Pairwise Catch Up

Solution Details

Reveal Pairwise Catch Up: intuition, complexity, and code

Hints

Hint 1
For each car compute its solo arrival time as (target - position) / speed: without traffic that is exactly when it would arrive.
Hint 2
A car starting behind joins an ahead fleet exactly when its solo time is less than or equal to that fleet arrival time.
Hint 3
Sort nearest-first. New fleet times grow strictly, so the nearest fleet ahead has the largest time already recorded.

Edge Cases

  • A single car: nothing can catch it and it catches nothing, so the answer is 1.
  • Equal solo times for neighbours, such as times 1 and 1: the behind car arrives exactly with the ahead one and they form one fleet.
  • A very slow car ahead of many fast cars: every fast car catches the same fleet, so the answer stays 1 for the group.
  • Input positions may be unsorted: keep position and speed paired while sorting; sorting just position invents different cars.
  • Equal speeds preserve all initial gaps, so every car remains its own fleet.
  • Several fast rear cars may merge first and then reach a slower front fleet; the final bottleneck time determines their shared arrival.

Common Mistakes and Interview Tips

  • Using integer division for (target - position) / speed: times like 2.5 truncate and equal-time fleets get miscounted, so use floating-point division.
  • Sorting farthest-first and scanning forward: the bottleneck logic reads cleanly only nearest-first, where each new fleet time must beat the running maximum.
  • Starting a new fleet on >= instead of strictly >: an equal time means catching up exactly at the target, which is still one fleet.
  • Treating the first successful leader comparison as an actual group assignment: the baseline counts only whether a merge occurs; the nearest fleet controls the traffic.
  • Interview check: explain why only one maximum remains after sorting before claiming the scan is linear; total time still includes sorting.

Key Takeaway

For catch-up processes, sort by position and sweep from the destination outward, letting a single running extreme replace all pairwise comparisons.