#128 · Greedy · Advanced · 120 XP · about 20 min
You stand on the first square of a row. jumps[i] is the longest jump you can make from square i: any distance from 1 up to jumps[i] squares forward. Write min_jumps(jumps) that returns the smallest number of jumps that takes you to the last square.
You can assume the last square can always be reached.
min_jumps([1, 4, 1, 1, 1, 2]) # returns 2 min_jumps([2, 0, 3, 0, 0, 1]) # returns 2 min_jumps([1, 1, 1, 1]) # returns 3 min_jumps([0]) # returns 0
In the first example, hop onto the 4 and then jump straight to the last square: two jumps.
1 <= len(jumps) <= 100000 <= jumps[i] <= 1000Ready?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.