Car Fleet
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
target = 12
position = [8,4,0]
speed = [2,4,1]2Explanation
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
target = 10
position = [0,2,4]
speed = [3,2,1]1Explanation
Both cars behind the slower car at 4 catch its fleet, leaving one fleet.
Example 3
target = 6
position = [1]
speed = [2]1Explanation
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 contract
carFleetArguments: json, json, jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 35 cases, on your device.
12[8,4,0][2,4,1]