medium
Car Fleet
Cars start at distinct positions on a one-lane road heading to a target; each has a constant speed. A car that catches up to a slower car ahead cannot pass and instead joins it as a fleet moving at the slower speed. Count how many fleets arrive at the target.
Constraints
- 1 ≤ n ≤ 10^5
- 0 < target ≤ 10^6
- 0 ≤ position[i] < target, all distinct
- 0 < speed[i] ≤ 10^6
Examples
in: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
out: 3
Code it yourself
Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?
Choose an approach to check your pattern recognition, or reveal the discussion when you need help.