You walk right from position 0 to finish. Scooters are parked at the distinct positions in scooters (unsorted), each with battery for 10 units.
- Whenever you're on foot at a position with an unused scooter (including
0, and the spot where a ride just ended), you take it. - You ride right until it has covered 10 units or you reach
finish, ignoring scooters you pass. - You leave it there and continue on foot.
Write scooter_distance(finish: int, scooters: list[int]) -> int: the total distance travelled on scooters.
scooter_distance(23, [7, 4, 14, 20])
# 19: walk 0->4, ride 4->14 (passing 7), take the scooter at 14 and ride 14->23 (passing 20)
scooter_distance(5, [0]) # 5 (the battery would last longer, but the street ends)
scooter_distance(12, [15]) # 0 (the only scooter is beyond the finish)
scooter_distance(30, []) # 0
1 <= finish <= 10**9; up to 10**5 scooters at positions up to 10**9.
⭐ Bonus: stepping along the street one unit at a time still passes; a speed test on a street a billion units long earns a star if you jump from scooter to scooter (O(n log n)).
Show hint
Sort the scooters. From your position pos, the next scooter is the first at or after pos (binary search, or a pointer that only moves forward). If it's at or past finish, stop; otherwise add min(10, finish - s) and jump to the ride's end.