Fewest Jumps to the End
Same jumping rule (from i you may advance up to ai), but now print the minimum number of jumps to reach position N, or −1 if it is unreachable.
Input: the first line has N; the second line has N non-negative integers.
Output: the minimum number of jumps, or −1.
Constraints:
- 1 ≤ N ≤ 105
- 0 ≤ ai ≤ 105
Sample Tests
Input 1
5 2 3 1 1 4
Output 1
2
Input 2
5 3 2 1 0 4
Output 2
-1
Discussion (opens after you solve — no spoilers before)
No comments yet.