Jump Game II

Given an array nums where nums[i] represents the maximum jump length from index i, return the minimum number of jumps needed to reach the last index. You can assume the last index is always reachable. Pattern focus: Jump Game. Track the current jump range and the farthest point reachable with one additional jump, then extend the range when you finish the current one.

Input Format

nums = maximum jump lengths at each index

Output Format

minimum number of jumps to reach the last index

Constraints

  • 2 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
  • The last index is reachable.

Examples

Example 1:

Input:

nums = [2,3,1,1,4]

Output:

2

Explanation:

Jump from index 0 to index 1, then to the last index.

Example 2:

Input:

nums = [2,3,0,1,4]

Output:

2

Explanation:

A minimum of two jumps is enough: 0 -> 1 -> 4.

Example 3:

Input:

nums = [1,1,1,1]

Output:

3

Explanation:

You must move one step at a time.

Loading...
Jump Game II - Greedy DSA Problem