Car Fleet

Medium

Topics
ArrayStackSortingMonotonic Stack

Cars head to a destination at target. Car i starts at position[i] with constant speed[i]; a faster car catches up to a slower one ahead and then travels as a fleet at the slower speed. Return the number of car fleets that arrive.

Example 1

012[10,12][8,12][5,12][3,12][0,12]
fleet leader
Lanes are sorted nearest-the-target first; each runs from a car’s position to the target at 12. A faster car cannot pass a slower one, it just joins its fleet. So a shaded lane is a car that arrives later than everything ahead of it and therefore starts a new fleet — 3 fleets here.
Positions and speeds are given as two parallel arrays; each lane above is one car’s remaining distance.
Input:  target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
Output: 3

Example 2

310[3,10]
fleet leader
Input:  target = 10, position = [3], speed = [3]
Output: 1

Constraints

  • 0 <= n <= 10^5
  • 0 < target <= 10^6
  • All positions are unique.
Editorial — intuition, approach, complexity and pitfalls

Intuition

Process cars from the one closest to the target backwards. Compute each car’s arrival time if unobstructed. A car that would arrive no later than the fleet ahead merges into it; otherwise it forms a new fleet.

Approach

  1. Sort cars by position descending.
  2. For each car compute time = (target - position) / speed.
  3. If its time is strictly greater than the current fleet’s lead time, it is a new fleet (update the lead time); otherwise it joins the fleet ahead.

Dry Run

target = 12   position = [10, 8, 0, 5, 3]   speed = [2, 4, 1, 1, 3]
Process from the car closest to the target backwards. `lead` is the arrival time
of the fleet currently in front.
  pos 10  speed 2   (12-10)/2 =  1.000   > lead      NEW fleet 1, lead 1.000
  pos  8  speed 4   (12- 8)/4 =  1.000   <= 1.000    catches up, joins fleet 1
  pos  5  speed 1   (12- 5)/1 =  7.000   >  1.000    NEW fleet 2, lead 7.000
  pos  3  speed 3   (12- 3)/3 =  3.000   <= 7.000    catches up, joins fleet 2
  pos  0  speed 1   (12- 0)/1 = 12.000   >  7.000    NEW fleet 3, lead 12.000
answer 3
The car at pos 8 is the row to understand. Left alone it would arrive at exactly
1.000, the same moment as the car ahead, so it has caught up and merges. That is
why the test is > and not >=: arriving simultaneously is arriving together.
Note lead is NOT updated when a car merges. A fleet moves at the speed of its
slowest member, which is the car in front, so a faster car joining from behind
cannot change when the fleet arrives. On this example updating it anyway happens
to give the same answer, but it is wrong in general -- on target 12 with
position [9,5,4,2,8] and speed [1,3,4,1,3] the answer is 2 and updating lead on
every car gives 3. Across 20000 random inputs the two disagree 2470 times.
The times are real-valued and compared directly, which is fine here because they
come from exact small integers. A division-free form, comparing
(target - p1) * s2 against (target - p2) * s1, avoids the question entirely.

Complexity

  • Time — O(n log n): sorting by position dominates.
  • Space — O(n): the sorted list.

Common Pitfalls

  • A car catching up merges — only a strictly larger arrival time starts a new fleet.

Full Solution — all languages

class Solution:
    def carFleet(self, target, position, speed):
        idx=sorted(range(len(position)),key=lambda i:-position[i])
        fleets=0; lead=-1.0
        for i in idx:
            t=(target-position[i])/speed[i]
            if t>lead:
                fleets+=1; lead=t
        return fleets
Asked at Nutanix PhonePe
SensAI
Beta
Tap the mic to stop voice mode