DIY: Jump Game II

Solve the interview question "Jump Game II" in this lesson.

Problem statement

Given an array of non-negative integers nums, you are initially at the first index of the array. Each element in the array represents your maximum jump length from that position/index.

Your goal is to reach the last index in the minimum number of jumps. Reaching the last index is always possible.

Input

The input will be an array of integers. The following is an example input:

nums = {4,1,1,3,1,1,1}

Output

The output will be an integer representing the minimum number of jumps. The following is an example output:

2

Jump 3 steps from index 0 to 1, then jump 3 steps to the last index.

Coding exercise

Implement the jump(nums) function, where nums is the array of integers. The function returns the minimum number of jumps to reach the last index from the first.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.