Problem Statement:
You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.
Examples:
Example 1:
Input: nums = [2,3,1,1,4]
Output: true
Explanation:
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [3,2,1,0,4]
Output: false
Explanation:
You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
Constraints
1 <= nums.length <= 104
0 <= nums[i] <= 105
Time Complexity:
Time Complexity = O(n)
Space Complexity:
Space Complexity = O(1)
Approach
- Maintain a variable farthest to track the
farthest indexreachable so far. Traversethe array from left to right.- If the current index i is greater than farthest, it means this position cannot be reached, so
return false. - Otherwise,
update the farthest reachableindex using:farthest = Math.max(farthest, i + nums[i]). - Continue updating the reachable range while
traversing the array. - If the
loop finisheswithout getting stuck, it means the last index is reachable.
Dry Run
Input: nums = [2, 3, 1, 1, 4]
Initialization: farthest = 0 Step 1: i = 0 Check: i > farthest ? 0 > 0 → false (we can reach this index) Update farthest: farthest = max(0, 0 + nums[0]) farthest = max(0, 2) farthest = 2 Step 2: i = 1 Check: i > farthest ? 1 > 2 → false Update farthest: farthest = max(2, 1 + nums[1]) farthest = max(2, 4) farthest = 4 Step 3: i = 2 Check: i > farthest ? 2 > 4 → false Update farthest: farthest = max(4, 2 + nums[2]) farthest = max(4, 3) farthest = 4 Step 4: i = 3 Check: i > farthest ? 3 > 4 → false Update farthest: farthest = max(4, 3 + nums[3]) farthest = max(4, 4) farthest = 4 Step 5: i = 4 Check: i > farthest ? 4 > 4 → false Update farthest: farthest = max(4, 4 + nums[4]) farthest = max(4, 8) farthest = 8 Loop finished successfully without failing the condition.
Output: true
var canJump = function(nums) {
let farthest = 0;
for(let i = 0; i < nums.length; i++) {
if(i > farthest) return false;
farthest = Math.max(farthest, i + nums[i]);
}
return true;
};
def canJump(nums):
farthest = 0
for i in range(len(nums)):
if i > farthest:
return False
farthest = max(farthest, i + nums[i])
return True
class Solution {
public boolean canJump(int[] nums) {
int farthest = 0;
for(int i = 0; i < nums.length; i++) {
if(i > farthest) return false;
farthest = Math.max(farthest, i + nums[i]);
}
return true;
}
}
#include <iostream>
class Solution {
public:
bool canJump(vector& nums) {
int farthest = 0;
for(int i = 0; i < nums.size(); i++) {
if(i > farthest) return false;
farthest = max(farthest, i + nums[i]);
}
return true;
}
};
#include <stdbool.h>
bool canJump(int* nums, int numsSize) {
int farthest = 0;
for(int i = 0; i < numsSize; i++) {
if(i > farthest) return false;
if(farthest < i + nums[i]) {
farthest = i + nums[i];
}
}
return true;
}
public class Solution {
public bool CanJump(int[] nums) {
int farthest = 0;
for(int i = 0; i < nums.Length; i++) {
if(i > farthest) return false;
farthest = Math.Max(farthest, i + nums[i]);
}
return true;
}
}
