Back to problems Sign in to unlock
Leetcode
Medium
Arrays
Greedy
Dynamic Programming
Google
Jump Game II
Find the minimum number of jumps needed to reach the last index of an array.
Acceptance 100%
Problem Statement
You are given an integer array nums where nums[i] represents the maximum distance you can jump forward from index i.
Start at index 0. In one move, you may jump from index i to any index j such that i < j <= i + nums[i].
Return the minimum number of jumps required to reach the last index of the array.
Input Format
- An integer array
numsof lengthn. nums[i]is the maximum forward jump length from indexi.
Output Format
- Return a single integer: the minimum number of jumps needed to reach index
n - 1.
Constraints
1 <= nnums.length = n- The last index is reachable.
- Values are non-negative integers.
Examples
Sample cases returned by the problem API.
Example 1
Input
nums = [2,3,1,1,4]
Output
2
Explanation
Jump from index 0 to index 1, then from index 1 to index 4.
Example 2
Input
nums = [2,3,0,1,4]
Output
2
Explanation
One optimal path is 0 -> 1 -> 4.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.