Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

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 ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access