Sharing my simple java solution


  • 1
    D

    public int rob(int[] nums) {

       int tmp1=0,tmp2=0,tmp3=0;
    	int max=0;
    	for(int i=0; i<nums.length; i++){
    		int tmp = Math.max(tmp1+nums[i], tmp2+nums[i]);
    		tmp1 = tmp2;
    		tmp2 = tmp3;
    		tmp3 = tmp;
    		
    		max = Math.max(max, tmp);
    	}
    	
    	return max;
    }

Log in to reply
 

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