Convoys at the finish line
Cars drive along a one-lane road toward a finish line at point target. Car i starts at point position[i] and drives at a constant speed[i]. No car can pass another: when a car catches up with a slower one ahead, it slows down and both go on as one convoy at the speed of the car in front. A lone car is a convoy too, and cars that meet exactly at the finish line arrive as one convoy. Return how many convoys cross the finish line.
Constraints: both lists have the same length, possibly 0; positions are distinct, 0 <= position[i] < target <= 10^6, 1 <= speed[i] <= 100. Aim for O(n log n).
Besides the tests, one large input (3,500 cars) with a ~200 ms limit (scaled to your device's speed): brute force won't pass.
Examples
count_convoys(20, [14,10,2,6], [2,4,1,3])→3count_convoys(100, [0,10,20,30], [4,3,2,1])→1count_convoys(12, [0,6], [2,1])→1