Jump game II
Standard · Write code · about 15 min
You start at index 0 of nums. From index i you may jump forward by any length from 1 to nums[i].
Return the fewest jumps that reach the last index. The input always allows reaching it.
Example 1
Input:
[[2,3,1,1,4]]
Output:
2
Jump 0 → 1, then 1 → 4.
Example 2
Input:
[[2,3,0,1,4]]
Output:
2
Jump 0 → 1, then 1 → 4.
Constraints
- 1 ≤ nums.length ≤ 10,000
- 0 ≤ nums[i] ≤ 1,000
- The last index is reachable
Learn the idea first: 1-D DP: house robber, decode ways, jump game II, a visual lesson.
More 1-D DP: house robber, decode ways, jump game II problems
- Fill the house robber table Warm-up
- House robber Standard
- Decode ways Standard
- Minimum cost for tickets Standard
- House robber II: houses in a circle Challenge