Car Fleet
Cars drive toward a destination at position target on a one-lane road. Car i starts at position[i] moving at speed[i]; a faster car that catches a slower one ahead cannot pass and joins it, forming a fleet that then moves at the slower car's speed. Cars arriving at the destination together count as one fleet. Return how many distinct fleets reach the target.
Open official problem prompt ↗Count how many groups of cars ultimately reach the destination as distinct clusters, given that faster cars stack up behind slower ones.
Traffic on a single-lane road with no passing: a speeding car eventually tailgates a slowpoke ahead and is forced to crawl behind it. From the finish line you only count the distinct bumper-to-bumper clusters that cross.
- Input
- target = 12, position = [10, 8, 0, 5, 3], speed = [2, 4, 1, 1, 3]
- Output
- 3
- Why
- Cars at 10 and 8 both reach the target at time 1 and form one fleet; the cars at 5 and 3 merge into a second fleet; the car at 0 arrives last as a third fleet.
n == position.length == speed.length1 <= n <= 10^50 < target <= 10^60 <= position[i] < target0 < speed[i] <= 10^6All position values are unique