45. Jump Game II
Last updated
Last updated
/**
* Time complexity :
* Space complexity :
*/
class Solution {
public int jump(int[] nums) {
if(nums == null || nums.length <= 1) {
return 0;
}
int jumps = 0, reach = 0, lastReach = 0;
for(int i = 0; i <= reach && i < nums.length; i++) {
if(i > lastReach) {
jumps++;
lastReach = reach;
}
reach = Math.max(reach, nums[i]+i);
}
if(reach < nums.length-1) {
return 0;
}
return jumps;
}
}