You are given an array of non-negative integers nums, where each element represents the maximum jump length from that position.
Starting at index 0, your task is to return the minimum number of jumps needed to reach the last index. Return -1 if the last index cannot be reached.
Input: nums = [2,3,1,1,4]
Output: 2
Explanation: Jump from index 0 to index 1, then from index 1 to the last index.
Input: nums = [1,1,1,1]
Output: 3
Explanation: Each jump advances one step, requiring three jumps to reach the end.
Input: nums = [1,0,3]
Output: -1
Explanation: The jump length of 0 at index 1 makes the last index unreachable.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
2 3 1 1 4
2
1 1 1 1
3
3 2 1 1 4
2