v9.1.1
Menu

Jump Game II

mediumPRODynamic ProgrammingGoogle DSA

Given a 0-indexed array nums where nums[i] represents the maximum jump length from index i, return the minimum number of jumps to reach the last index.

You can assume you can always reach the last index.

Example

Input
2,3,1,1,4
Output
2
Reasoning
  • We start at index 0 with a jump length of 2, allowing us to reach indices 1 or 2.
  • From index 1, we have a jump length of 3, which enables us to reach indices 2, 3, or 4, but we can only reach index 4 in two jumps if we first jump to index 1 and then to index 4.
  • The optimal path is 0→1→40 \rightarrow 1 \rightarrow 4, resulting in a total of 2 jumps.
  • This path is the minimum number of jumps required to reach the last index, so the output is 2.

Constraints

  • 1 <= len(nums) <= 10^4
  • 0 <= nums[i] <= 1000

Learn this in

More from Google DSA

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.