My Java solution


  • 2
    O
    public class Solution {
    public int rob(int[] nums) {
    	int size = nums.length;
    	
    	//Basic cases
    	if(size == 0) {
    		return 0;
    	} else if(size == 1) {
    		return nums[0];
    	} else if(size == 2) {
    		return Math.max(nums[0], nums[1]);
    	}
    	nums[2] += nums[0];
    	for (int i = 3; i < size; i++) {
    		nums[i] = Math.max(nums[i] + nums[i-3], nums[i] + nums[i-2]);
    	}
    	return Math.max(nums[size-1], nums[size-2]);
    }
    

    }


Log in to reply
 

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