This content originally appeared on DEV Community and was authored by Rakesh Reddy Peddamallu
so this is a greedy approach
our goal is to reach the last index , so we try to reach to index 0 and if we are able to make it , then we return True otherwise False
coming from back makes it to become a sub problem
Example - [2,3,1,1,4]
since anyways we can jump from 1 to 4
this again gets converted to
this again gets converted to
So now my goal is at index 0 which means , u can jump to last index if u begin from 0 index too
CODE
/**
* @param {number[]} nums
* @return {boolean}
*/
var canJump = function(nums) {
let goal = nums.length-1 ;
for(let i = nums.length-1 ;i>=0 ;i--){
if(i+nums[i] >= goal){
goal = i;
}
}
return goal==0
};
This content originally appeared on DEV Community and was authored by Rakesh Reddy Peddamallu

Rakesh Reddy Peddamallu | Sciencx (2025-01-25T16:48:47+00:00) Leetcode – 55. Jump Game. Retrieved from https://www.scien.cx/2025/01/25/leetcode-55-jump-game/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.