Back to ArraysJump Game II
Medium

Jump Game II

MediumRecommendedGreedy Array Patterns

Given an array where each element represents your maximum jump length from that position, return the minimum number of jumps needed to reach the last index (a valid path is always guaranteed to exist).

Examples

Example 1

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

Explanation: Jump 1 step from index 0 to index 1, then 3 steps from index 1 to the last index — 2 jumps total.

Example 2

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

Explanation: Jump from index 0 to index 1 (within the first range), then from index 1 directly to index 4.

Example 3

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

Explanation: Every element only allows a single step, so reaching the last index takes exactly 3 one-step jumps.

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 1000
  • It is guaranteed that you can reach the last index.

Loading editor…

Code execution is coming soon.