Ctrl K

Convoys at the Finish

Medium
Several cars drive along a straight road towards the mark finish. Car i starts at mark start[i] and drives at speed[i] marks per hour. The road has a single lane, so no overtaking. When a car catches up with the one ahead, they continue together at the speed of the front car, forming a convoy. A lone car is a convoy too. A car that catches a convoy exactly at mark finish counts as part of it. Return how many convoys reach the finish.
Example 1
finish=20, start=[2, 14, 10, 0], speed=[3, 1, 4, 2] → 2

The car at 14 needs 6 hours; the one at 10 needs 2.5 and catches it exactly at the finish. The car at 2 would need 6 hours too, so it joins them. The car at 0 needs 10 hours and arrives alone. That is 2 convoys.

Example 2
finish=9, start=[1, 5], speed=[1, 4] → 2

The front car arrives after 1 hour, the rear one after 8. Nobody catches up: 2 convoys.

Example 3
finish=50, start=[7], speed=[10] → 1
Constraints
  • 1 ≤ len(start) = len(speed) ≤ 10⁵
  • 0 < finish ≤ 10⁶
  • 0 ≤ start[i] < finish, all start[i] are distinct
  • 0 < speed[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFix the Middle of the Line MediumMost Pieces Sorted Separately MediumOutclassed Heroes MediumPeaks in Plain Sight MediumLongest Drop From End to End Medium
loading Python…
Input
finish=20, start=[2, 14, 10, 0], speed=[3, 1, 4, 2]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 125 tests on the server, hidden ones included.