DEV Community

Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

Leetcode - 45. Jump Game II

The question says that we can be always able to reach the end , only that we need to find is the min steps we can take.

We need 0 jumps to be at index 0 since we are starting there

Image description

Now from 2 we can jump to either jump to 3 or 1
Image description

we can represent this window by l , r pointers
Image description

now the next jump window is from 1 to 4 ( since this is the point they can jump max from window [3,1]
Image description

CODE:

/**
 * @param {number[]} nums
 * @return {number}
 */
var jump = function(nums) {
    let result = 0 ;
    let l = 0 ;
    let r = 0;
    while(r<nums.length-1){

        let farthest = 0 ;
        for(let i=l;i<=r;i++){
            farthest = Math.max(farthest , i+ nums[i]);
        }
        l = r + 1 ;
        r = farthest;
        result++;
    }
    return result
};
Enter fullscreen mode Exit fullscreen mode

Please go through the video by neetcode if you do not understand

Billboard image

Monitor more than uptime.

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay