Description

Given an array where nums[i] is the maximum jump length from position i, return the minimum number of jumps to reach the last index.

Examples

Input:nums = [2,3,1,1,4]
Output:2
Explanation:

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

Input:nums = [1,1,1,1]
Output:3
Explanation:

At index 0, nums[0]=1 allows jumping only to index 1. At index 1, nums[1]=1 allows jumping only to index 2. At index 2, nums[2]=1 allows jumping only to index 3. Path: 0→1→2→3 requires exactly 3 jumps. With all values being 1, there's no choice at each step.

Input:nums = [5,9,3,2,1,0,2,3,3,1,0,0]
Output:3
Explanation:

From index 0 (value 5), we can reach indices 1-5. Jumping to index 1 (value 9) gives maximum reach. From index 1, we can jump up to 9 positions, reaching index 10 (value 0). From index 10, we need one more jump to reach index 11. Path: 0→1→10→11 uses 3 jumps. Greedy selection of maximum-reach positions minimizes jumps.

Constraints

  • 1 ≤ nums.length ≤ 10⁴
  • 0 ≤ nums[i] ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!