Overview
Given an array of non-negative integers nums, you are initially positioned at the first index. Each element represents the maximum jump length from that position.
Determine if you can reach the last index.
Implement canReachEnd(nums).
Constraints
- Input is a non-empty array of non-negative integers.
- You start at index
0. - Each element
nums[i]represents the maximum forward jump from indexi(you can jump any distance from 1 tonums[i]). - Aim for $O(n)$ time and $O(1)$ space — a greedy approach is optimal.
Examples
canReachEnd([2, 3, 1, 1, 4]); // true — jump 1→2→3→4 or 1→3→4 canReachEnd([3, 2, 1, 0, 4]); // false — stuck at index 3 canReachEnd([0]); // true — already at the end canReachEnd([2, 0, 0]); // true — jump directly from 0 to 2
Notes
- Greedy approach: track the farthest index you can reach. Iterate through the array — at each position, update the farthest reachable index. If you ever land on an index beyond your reach, return
false. - At each index
i, ifi <= farthest, thenfarthest = max(farthest, i + nums[i]). - If
farthest >= nums.length - 1, returntrueearly. - This is a classic greedy problem that avoids the exponential cost of trying all jump combinations.
Solution
Reveal solution
function canReachEnd(nums) {
let farthest = 0;
for (let i = 0; i < nums.length; i++) {
if (i > farthest) return false;
farthest = Math.max(farthest, i + nums[i]);
if (farthest >= nums.length - 1) return true;
}
return true;
}Resources
end-of-array-reachable.js
End of Array Reachable
hardcodingAlgorithmsData Structures
Overview
Given an array of non-negative integers nums, you are initially positioned at the first index. Each element represents the maximum jump length from that position.
Determine if you can reach the last index.
Implement canReachEnd(nums).
Constraints
- Input is a non-empty array of non-negative integers.
- You start at index
0. - Each element
nums[i]represents the maximum forward jump from indexi(you can jump any distance from 1 tonums[i]). - Aim for $O(n)$ time and $O(1)$ space — a greedy approach is optimal.
Examples
canReachEnd([2, 3, 1, 1, 4]); // true — jump 1→2→3→4 or 1→3→4 canReachEnd([3, 2, 1, 0, 4]); // false — stuck at index 3 canReachEnd([0]); // true — already at the end canReachEnd([2, 0, 0]); // true — jump directly from 0 to 2
Notes
- Greedy approach: track the farthest index you can reach. Iterate through the array — at each position, update the farthest reachable index. If you ever land on an index beyond your reach, return
false. - At each index
i, ifi <= farthest, thenfarthest = max(farthest, i + nums[i]). - If
farthest >= nums.length - 1, returntrueearly. - This is a classic greedy problem that avoids the exponential cost of trying all jump combinations.
Solution
Reveal solution
function canReachEnd(nums) {
let farthest = 0;
for (let i = 0; i < nums.length; i++) {
if (i > farthest) return false;
farthest = Math.max(farthest, i + nums[i]);
if (farthest >= nums.length - 1) return true;
}
return true;
}Resources
NameTopicDifficulty
103 of 103 problems