Problem
You start at index 0 of the integer array jumps. From index i, choose an integer distance d and move to index i + d, provided that:
-
0 <= d <= jumps[i]
-
i + d < jumps.length
Return the fewest jumps needed to arrive at the final index. Every allowed input guarantees that this index is reachable.
Examples
Example 1
Input: jumps = [4,1,2,1,3,0,1,0]
Output: 2
One jump can move from index 0 to index 4, and the next can move from index 4 to index 7. A single jump from the starting position cannot reach index 7.
Example 2
Input: jumps = [0]
Output: 0
The starting index is already the final index, so no jump is needed.
Constraints
-
1 <= jumps.length <= 10^4
-
0 <= jumps[i] <= 1000
-
The final index is reachable from index
0
under the stated jump rules.