Skip to main content

853. Car Fleet

medium

Count how many car fleets reach a target, given staggered starting positions and speeds. Looks like simulation; collapses to a clean monotonic-stack on arrival times.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

There are n cars going to the same destination along a one-lane road. The destination is target miles away. You are given two integer array position and speed, both of length n, where position[i] is the position of the ith car and speed[i] is the speed of the ith car (in miles per hour). A car can never pass another car ahead of it, but it can catch up to it and drive bumper to bumper at the same speed. The faster car will slow down to match the slower car's speed. The distance between these two cars is ignored (i.e., they are assumed to have the same position). A car fleet is some non-empty set of cars driving at the same position and same speed. Note that a single car is also a car fleet. If a car catches up to a car fleet right at the destination, it will still be considered as one car fleet. Return the number of car fleets that will arrive at the destination.

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

Explanation: The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The car starting at 0 does not catch up to any other car, so it is a fleet by itself. The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target. Note that no other cars meet these fleets before the destination, so the answer is 3.

Example 2

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

Example 3

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

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Hints

Progressive — try the first before opening the next.

Hint 1

Compute each car's solo arrival time: (target - position) / speed.

Hint 2

Sort cars by starting position descending (closest to target first).

Hint 3

Iterate in that order. A car forms a new fleet if its solo time is strictly greater than the current leading fleet's time — otherwise it joins the leading fleet (it would catch up before target).

Hint 4

A stack of fleet arrival times makes the logic obvious — but you only ever need the top, so a single 'leadTime' variable works too.

Solution approach

Reveal approach

Pair each car as (position, time = (target - position) / speed). Sort by position descending. Scan in that order tracking the current leading fleet's arrival time. For each car: if its solo arrival time is strictly greater than the current leader's time, it can't catch up — it becomes a new fleet and updates the leader. Otherwise it joins the leader (gets capped at the leader's speed by the catch-up rule). Return the number of distinct leaders. O(n log n) time (dominated by the sort), O(n) space. The 'stack' framing pushes times that aren't absorbed by the previous top; the size of the stack is the answer.

Complexity

Time
O(n log n)
Space
O(n)

Related patterns

  • stack
  • monotonic-stack
  • sorting
  • greedy

Related problems

Asked at

Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).

  • Amazon
  • Google

Practice these live with InterviewChamp.AI

Drill Car Fleet and Stacks problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →