← 返回 pinterest 的题目列表Scooter Hopping on a Number Line (Greedy Simulation)
类型:online_judge
Problem: Total Scooter Riding Distance to Reach the Finish
You start at position 0 on a number line and need to reach finish (finish > 0).
There are electric scooters along the street, given by an integer array scooters, where scooters[i] is the location of the i-th scooter (all locations are distinct).
Each scooter can travel at most 10 units to the right from its starting location (inclusive). For example, a scooter at 5 can reach any point up to 15 (inclusive), but not 16 or beyond.
You must follow this exact procedure:
From your current position, walk to the nearest scooter on the right (the smallest scooter position strictly greater than your current position). If no scooters remain to the right, walk directly to finish.
Ride that scooter using all its battery, traveling as far right as possible (up to 10 units, but not beyond finish).
If you have not reached finish, repeat from step 1.
Return the total distance traveled while riding scooters by the time you reach finish.
Input
finish: integer
scooters: integer array
Output
integer: total riding distance on scooters
Constraints (from visible screenshot parts)
1 ≤ finish ≤ 1000
All values in scooters are distinct (other constraints not fully visible)
Example 1
Input: finish = 23, scooters = [7, 4, 14]
Output: 19
Explanation:
Walk from 0 to 4, ride from 4 to 14 (10 units)
At 14, the nearest scooter on the right is at 14, ride from 14 to 23 (9 units)
Total riding distance: 10 + 9 = 19
Example 2
Input: finish = 27, scooters = [15, 7, 3, 10]
Output: 20
Example 3
Input: finish = 10, scooters = []
Output: 0
Example
Input
23
3
7 4 14
Output
19