CodeSpeek

Jump Game II

Medium · Greedy

You are given an array nums where nums[i] is the maximum distance you can jump forward from index i. You start at index 0. Return the smallest number of jumps needed to reach the last index. You are guaranteed that the last index is always reachable.

Examples

Input:  nums = [2,3,1,1,4]
Output: 2
Why:    Jump 1 step from index 0 to 1, then 3 steps to the last index.
Input:  nums = [2,3,0,1,4]
Output: 2
Why:    Index 2 is a dead end, but you never have to land on it.

Constraints

1 <= len(nums) <= 10^4, 0 <= nums[i] <= 1000, and the last index is always reachable.

Practise it by voice

Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.

Practise Jump Game II

This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Jump Game II. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.