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

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

Example 2

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

Constraints

  • 0 <= n <= 10^5
  • 0 < target <= 10^6
  • All positions are unique.
Run ⌘' · Submit ⌘⏎