Java 1ms solution, that is simple to understand


  • 0
    N

    Click on the image to read the comments

    0_1503105024600_7c937f1f-6567-41d2-b254-240b256dc7c4-image.png

     public  int rob (int [] nums) {
    		
            if (nums == null){return -1;}
    		
    		if (nums.length == 0){ return 0;}
    		
    		if (nums.length == 1) {return nums[0];}
    		
    		int [] weight = new int [nums.length];
    		
    		for (int i =0; i <nums.length;i++){
    			
    			weight [i] = nums[i] + weightCompute (weight,i);
    			
    		}
    
            
            if (weight [ nums.length-1] >  weight [ nums.length-2] ){
                
                return weight [ nums.length-1];
            }
            
                return weight [ nums.length-2] ;
    		
    		
    		
    	}
        
      public  int weightCompute (int nums [], int id ){
    		
    			int i = id -2;
    	
    			if (i <0){ 
    				return 0;
    				}
    			
    			int max = -1;
    		
    			for (; i>=0; i--){
    			
    				if (max < nums[i]){
    				
    					max = nums[i];
    				}
    			
    		}
    		
    			return max;
    		
    	}
    

Log in to reply
 

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