Java simple o(n) 6ms


  • 0
    R
    public int jump(int[] nums) {
        if(nums==null||nums.length<2){
            return 0;
        }
        int result = 1;
        int previous = nums[0], currentMax =  0;
        for (int index = 1; index < nums.length; index++) {
            --previous;
            currentMax = Math.max(nums[index], --currentMax);
            if(previous==0&&index<nums.length-1){
                previous=currentMax;
                result++;
                if((currentMax+index+1)==nums.length){
                    return result;
                }
            }
        }
        return result;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.