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
Now from 2 we can jump to either jump to 3 or 1
we can represent this window by l , r pointers
now the next jump window is from 1 to 4 ( since this is the point they can jump max from window [3,1]
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
};
Please go through the video by neetcode if you do not understand
Top comments (0)