DEV Community

Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

Leetcode - 55. Jump Game

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]

Image description

the above can converted to
Image description

since anyways we can jump from 1 to 4

this again gets converted to
Image description

this again gets converted to

Image description

this again gets converted to

Image description

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
};
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs