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
};
Top comments (0)