Convoy Count
mediumDelivery trucks drive down a single-lane highway toward a depot at mile target. Truck i starts at mile positions[i] (all distinct) moving at speeds[i] miles per hour.
The lane allows no overtaking: when a faster truck catches up to a slower one ahead, it slows to match and they travel on as one convoy (a convoy is also a single truck that never catches anyone). If a truck catches another exactly at the depot, they count as one convoy.
Return the number of distinct convoys that arrive at the depot.
Example 1
in target = 12, positions = [10, 8, 0, 5, 3], speeds = [2, 4, 1, 1, 3]
out 3
Trucks at 10 and 8 merge at mile 12; the truck at 0 rides alone; trucks at 5 and 3 merge into a second convoy.
Example 2
in target = 10, positions = [3], speeds = [3]
out 1
Constraints
- · 1 <= len(positions) == len(speeds) <= 10^5
- · 0 <= positions[i] < target <= 10^6, all positions distinct
- · 1 <= speeds[i] <= 10^6
solution.py● loading python…
test results
Hit ▶ Run to test your code against the visible cases.