629. Car Fleet

MediumStackMonotonic StackSimulation

Cars head to a target at given positions with given speeds; a faster car catching a slower one ahead forms a fleet that then moves at the slower car's speed (cars never pass). Return the number of fleets that arrive at the target. The input is JSON {target, position, speed}.

Input: JSON {target, position, speed}.

Output: Integer — the number of car fleets.

Examples

Example 1
Input: {"target":12,"position":[10,8,0,5,3],"speed":[2,4,1,1,3]}
Output: 3
Explanation: Cars merge into 3 fleets.
Example 2
Input: {"target":10,"position":[3],"speed":[3]}
Output: 1
Explanation: A single car.
Example 3
Input: {"target":100,"position":[0,2,4],"speed":[4,2,1]}
Output: 1
Explanation: All merge into one.

Constraints

Asked by

GoogleAmazonMicrosoftBloombergMeta
Solve this problem in the editor →