Skip to content
BASA DSA

Car Fleet

MediumStack

Cars move toward target from distinct positions with their given positive speeds. A faster car cannot pass a slower car ahead; after catching it, they travel as one fleet. Return the number of fleets reaching target, including catches at target.

Example 1

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

Explanation

Cars at 8 and 4 both arrive in two hours and merge at the target; the car at 0 takes twelve hours and stays separate.

Example 2

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

Explanation

Both cars behind the slower car at 4 catch its fleet, leaving one fleet.

Example 3

Input
target = 6
position = [1]
speed = [2]
Output
1

Explanation

A single car forms one fleet.

Constraints

  • 1 <= number of cars <= 100000
  • 0 <= position[i] < target <= 1000000
  • 1 <= speed[i] <= 1000000
  • Position and speed arrays have equal lengths

Function contractcarFleetArguments: json, json, json
Loading draft…
SolutionRuns on your device
Loading draft…