Given a number (finish end point), then an array of scooters, where scooters represents the position of the
ith scooter.Each scooter can travel up to 10 points before the battery is fully discharged, and cannot go further. For example, if the scooter is at point 5, it can travel to points 5, 6, 7, ..., ., up to point 15 (inclusive), but not to point 16 or beyond.
Calculate walking steps to reach the target.
Example1:
finish=23, scooters[7, 4, 14]
output -> solution(finish, scooters) = 4
Explanation1:
- Starting from 0, the closest scooter is scooters[1] = 4 so it takes 4 points to walk there.
- Then the scooter can go up to 10 points, 10+4=14.
- There is a scooter at 14 points (scooters[2] = 14).
- This way we can go straight to the end to complete 23.
- So it's a total of 4 points of walking
Example2:
finish=27, scooters[15, 7, 3, 10]
output -> solution(finish, scooters) = 5
My Code:
finish=23
scooters = [7, 4, 14]
def solution(finish, scooters):
sum = min(scooters)
step = min(scooters)
while sum < finish:
step += 10
sum = sum + step
return step
solution(finish, scooters)
How to include scooters[i] within the while loop to check for the next available scooter?