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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

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

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay