Jump Game II
Given a 0-indexed array nums where nums[i] is the maximum forward jump length from index i, return the minimum number of jumps needed to reach the last index. The test cases guarantee the last index is always reachable.
Open official problem prompt ↗Find the fewest hops that carry you from the first index to the last, where each cell caps how far a single hop can go.
Like hopping across a river on stones where each stone tells you the maximum distance you may leap: from your current bank you scout every stone you can already reach, remember the one that lets you jump farthest, and only 'spend' a leap when you must cross beyond your current shore.
- Input
- nums = [2, 3, 1, 1, 4]
- Output
- 2
- Why
- Jump 1 step from index 0 to index 1, then 3 steps from index 1 to the last index.
1 <= nums.length <= 10^40 <= nums[i] <= 1000It is guaranteed that you can reach nums[n - 1]