55. Jump Game
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks whether it's possible to reach the last index of an array, starting from the first index. At any given index i, we can jump a maximum of nums[i] steps forward. This means from index i, we can reach any index j such that i <= j <= i + nums[i].
Solution
Jump Game in Python
class Solution:
def canJump(self, nums: List[int]) -> bool:
n = len(nums)
farthest = 0 # The farthest index we can reach
# Iterate through each index of the array
for i in range(n):
# If the current index `i` is beyond the farthest index we can reach,
# it means we are stuck and cannot proceed.
if i > farthest:
return False
# Update the farthest index we can reach by considering the jump from the current index.
farthest = max(farthest, i + nums[i])
# If the loop completes, it means we could reach or pass the last index.
return True
Jump Game in Java
class Solution {
public boolean canJump(int[] nums) {
int n = nums.length;
int farthest = 0; // The farthest index we can reach
// Iterate through each index of the array
for (int i = 0; i < n; i++) {
// If the current index `i` is beyond the farthest index we can reach,
// it means we are stuck and cannot proceed.
if (i > farthest) {
return false;
}
// Update the farthest index we can reach by considering the jump from the current index.
farthest = Math.max(farthest, i + nums[i]);
}
// If the loop completes, it means we could reach or pass the last index.
return true;
}
}
Jump Game in C++
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size();
int farthest = 0; // The farthest index we can reach
// Iterate through each index of the array
for (int i = 0; i < n; ++i) {
// If the current index `i` is beyond the farthest index we can reach,
// it means we are stuck and cannot proceed.
if (i > farthest) {
return false;
}
// Update the farthest index we can reach by considering the jump from the current index.
farthest = max(farthest, i + nums[i]);
}
// If the loop completes, it means we could reach or pass the last index.
return true;
}
};
Jump Game in TypeScript
function canJump(nums: number[]): boolean {
const n = nums.length;
let farthest = 0; // The farthest index we can reach
// Iterate through each index of the array
for (let i = 0; i < n; i++) {
// If the current index `i` is beyond the farthest index we can reach,
// it means we are stuck and cannot proceed.
if (i > farthest) {
return false;
}
// Update the farthest index we can reach by considering the jump from the current index.
farthest = Math.max(farthest, i + nums[i]);
}
// If the loop completes, it means we could reach or pass the last index.
return true;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.