/45. Jump Game II

45. Jump Game II

Medium
Arrays42.6% acceptance

Given an integer array jump_lengths of length n, where jump_lengths[i] represents the maximum forward jump length from index i, determine the minimum number of jumps required to reach the last index (n-1) starting from index 0. It is guaranteed that the last index is reachable from the first index.

Example 1

Input: [4,1,1,3,1,1,1]

Output: 2

Explanation: Jump from index 0 to index 3 (jump length 3), then from index 3 to index 6 (jump length 3).

Example 2

Input: [1,2,1,1,1]

Output: 3

Explanation: Jump from 0 to 1, 1 to 3, 3 to 4.

Example 3

Input: [5,2,1,0,0,0,1]

Output: 1

Explanation: Jump from 0 to 6 directly.

Constraints

  • 1 <= len(jump_lengths) <= 10_000
  • 0 <= jump_lengths[i] <= 1000
  • It is guaranteed that the last index is reachable from the first index
Python (current runtime)

Case 1

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

Expected: 3

Case 2

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

Expected: 3

Case 3

Input: [6,1,1,1,1,1,1]

Expected: 1

Case 4

Input: [1,1,1,1,1,1,1]

Expected: 6

Case 5

Input: [2,2,2,2,2]

Expected: 2