853. Car Fleet
Practice Car Fleet for free on NudgeCode: Medium, Stack pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
Cars drive along a one-lane road toward mile target. Car i starts at position[i] and drives at speed[i]. A car can't pass the one in front; when it catches up it slows down and they move together as one fleet. A car that catches another exactly at target still joins its fleet. Return how many fleets arrive at target.
Example
target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3] -> 3
The cars at 10 and 8 meet at 12; the car at 0 never catches anyone; the cars at 5 and 3 meet at 6.
Constraints
- 1 <= position.length <= 10^5,
speed.length == position.length - 0 < target <= 10^6
- 0 <= position[i] < target, and all positions are different
- 0 < speed[i] <= 10^6