Jump Game — Solution in Python, Java & C++
Jump Game solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem, asked at Morgan Stanley.
Jump Game is a medium coding problem on arrays, dynamic programming and greedy, asked in Morgan Stanley interviews.
- Difficulty: Medium
- Topics: Arrays, Dynamic Programming, Greedy
- Asked at: Morgan Stanley
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You are given an integer array nums. You start at the first index; each element is your maximum jump length from that position.
Return true if you can reach the last index.
Example 1
Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step to index 1, then 3 steps to the end.
Example 2
Input: nums = [3,2,1,0,4]
Output: false
Explanation: You always land on index 3, whose jump length is 0.
Constraints
1 <= nums.length <= 300 <= nums[i] <= 5
Hints for Jump Game
Hint 1
Track the furthest index reachable so far.
Hint 2
If the current index is beyond that reach, you're stuck.
How to solve Jump Game
You never need to know which jumps to take — only how far it is possible to get. Sweep left to right carrying a single number, reach, the furthest index any sequence of jumps could land on so far. If the sweep ever reaches an index beyond reach, that index is unreachable and the answer is no.
Approach
- Start with
reach = 0. - Walk
ifrom0to the last index. - If
i > reach, no jump sequence can get here — returnfalse. - Otherwise update
reach = max(reach, i + nums[i]). - Surviving the whole walk means the last index was reachable — return
true.
Why it works
The set of reachable indices is a prefix: if you can reach index i, you can reach every index below it, because a jump of length nums[i] allows any shorter hop too. So the reachable set is fully described by its maximum, reach, and a single number is enough state. Since i <= reach means i is genuinely reachable, extending with i + nums[i] is legitimate, and the first i exceeding reach is a real wall — everything beyond it is cut off as well.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- A
0in the array is only fatal if it sits exactly at the currentreach— zeros you can jump over are harmless. - Check
i > reachbefore usingnums[i], or you extend the reach from a position you never actually got to. - A single-element array is trivially
true: you are already at the last index.
Reference solution
Python
from typing import List
def canJump(nums: List[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
return TrueJava
public static boolean canJump(int[] nums) {
int reach = 0;
for (int i = 0; i < nums.length; i++) {
if (i > reach) return false;
if (i + nums[i] > reach) reach = i + nums[i];
}
return true;
}C++
bool canJump(vector<int>& nums) {
int reach = 0;
for (int i = 0; i < (int) nums.size(); i++) {
if (i > reach) return false;
if (i + nums[i] > reach) reach = i + nums[i];
}
return true;
}JavaScript
var canJump = function(nums) {
let reach = 0;
for (let i = 0; i < nums.length; i++) {
if (i > reach) return false;
reach = Math.max(reach, i + nums[i]);
}
return true;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Dynamic Programming