Minimum number of jumps to reach end | Jump Game II

Vipul Gupta
1 min readMay 11, 2021

This question is based on ladder and stairs, We need to find the minimum number of jumps to reach the end.

Here we will follow few steps:

  1. initialize stairs and ladder as the first element of the array.
  2. Keep track of the maximum ladder we got as of now.
  3. decrease the stairs as we climb up.
  4. if stairs become zero in the current ladder make a jump to another ladder and recalculate the stairs left in that ladder to reach the top(stairs = ladder level)

Below is the implementation of the code.

class Solution {
public int jump(int[] nums) {
if(nums.length<=1)
return 0;
int stairs = nums[0];
int ladder = nums[0];

int jumps = 1;
int len = nums.length;
for(int level =1 ;level<len;level++){
if(level == len-1) //[2,3,1,1,4] to check this particular case may be we are at last
return jumps;
if(level+nums[level]>ladder)
ladder= level+nums[level];
stairs--;
if(stairs == 0){
jumps++;
stairs = ladder-level;// get the remaining stairs on ladder
}

}
return jumps;
}
}

Happy Coding!

--

--