Simple and clean Java 3ms solution


  • 0
    V
    public class Solution {
        public int jump(int[] nums){
    	    int curVal=nums[0];
    	    int maxVal=nums[0];
    	    int jumps=1;
    	    int length=nums.length-1;
            if(length <=0){
                return 0;
            }	    
            for(int i=1;i<=maxVal;i++){
                if(i==length) return jumps;
                curVal=curVal>(nums[i]+i) ? curVal:(nums[i]+i);
                if(i == maxVal){
                    maxVal=curVal;
                    if(i>= curVal){
                        return -1;
                    } 
                    jumps++;
                }
            }
            return -1;
    	}
    }

Log in to reply
 

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