Jump Game II

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

Given a 0-indexed array of integers nums where each element represents the maximum length of a forward jump from that index, return the minimum number of jumps to reach the last index.

You can assume that you can always reach the last index.

Examples

Example 1:

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

Output: 2

Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

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

Output: 2

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

Example 3:

Input: nums = [1,2,3]

Output: 2

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

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 1000
  • It is guaranteed that you can reach nums[n - 1]
Source: Greedy Techniques pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle