You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].
Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:
0 <= j <= nums[i] andi + j < nReturn the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].
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
Constraints:
1 <= nums.length <= 1040 <= nums[i] <= 1000nums[n - 1].Problem Overview: You are given an array where nums[i] represents the maximum jump length from index i. Starting at index 0, the goal is to reach the last index using the minimum number of jumps. Every jump can move anywhere within the allowed range from the current position.
Approach 1: Dynamic Programming (O(n^2) time, O(n) space)
Dynamic Programming models the problem as a minimum steps problem. Create a dp array where dp[i] stores the minimum number of jumps needed to reach index i. Initialize dp[0] = 0 and iterate through each index. For every position i, iterate through all previous indices j and check if j + nums[j] >= i. If reachable, update dp[i] = min(dp[i], dp[j] + 1). This explores all valid jump paths and guarantees the optimal answer. Time complexity becomes O(n^2) because each index may scan all previous indices, while space complexity is O(n) for the DP table. This approach clearly demonstrates the optimal substructure typical in dynamic programming problems.
Approach 2: Greedy Range Expansion (O(n) time, O(1) space)
The optimal solution uses a greedy strategy that treats the array like levels in a BFS traversal. Maintain three variables: jumps, currentEnd, and farthest. As you iterate through the array, continuously update farthest = max(farthest, i + nums[i]) to track the farthest index reachable from the current jump window. When the iteration reaches currentEnd, you must make another jump, so increment jumps and update currentEnd = farthest. This effectively expands the reachable range layer by layer, similar to exploring levels in a graph. The algorithm scans the array once, giving O(n) time complexity and O(1) space complexity. This technique is a classic example of a greedy decision where the locally optimal choice (extending the reach as far as possible) produces the globally optimal result. It frequently appears in problems tagged under greedy and array traversal patterns.
Recommended for interviews: The greedy approach is the expected answer in most interviews. It reduces the quadratic DP exploration to a single linear pass while maintaining correctness. Explaining the DP approach first shows you understand the state transition and optimal substructure. Then optimizing it to the greedy window expansion demonstrates strong algorithmic reasoning and pattern recognition.
The greedy approach involves making a jump only when absolutely necessary. Track the maximum index you can reach at each step and make a decision to jump when you get to the maximum of the current window. This ensures the least number of jumps.
We maintain two variables: `farthest`, which tracks the farthest point reachable, and `currentEnd`, which tracks the end of the current jumping range. A jump is made when we reach `currentEnd`, which updates to `farthest`.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the number of elements in the array, because we make a single pass through the array.
Space Complexity: O(1), since we are using a fixed amount of extra space.
The dynamic programming approach calculates the minimum jumps required to reach each index. For each index, calculate the minimum number of jumps required from all previous indices that can reach the current index. However, this approach is less efficient in terms of time complexity.
Use an array `dp` to store the minimum number of jumps to reach each index. Initialize `dp[0]` with zero since no jumps are needed to reach it.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2), where n is the number of elements.
Space Complexity: O(n), due to the use of a DP array.
| Approach | Complexity |
|---|---|
| Greedy Approach | Time Complexity: O(n), where n is the number of elements in the array, because we make a single pass through the array. |
| Dynamic Programming Approach | Time Complexity: O(n^2), where n is the number of elements. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming | O(n^2) | O(n) | Useful for understanding state transitions and building intuition for minimum steps problems |
| Greedy Range Expansion | O(n) | O(1) | Best choice for interviews and large inputs where a linear pass is required |
Jump Game - Greedy - Leetcode 55 • NeetCode • 290,114 views views
Watch 9 more video solutions →Practice Jump Game II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor