My Java Solution


  • 1
    W
    public int max(int a, int b){
        return a>b?a:b;
    }
    public int rob(int[] nums) {
        if(nums==null) return 0;
        int len = nums.length;
        if(len==0) return 0;
        if(len==1) return nums[0];
        if(len==2) return max(nums[0], nums[1]);
        if(len==3) return max(nums[0]+nums[2], nums[1]);
        
        nums[2] += nums[0];
        for(int i=3; i<len; ++i){
            if(nums[i-2]>nums[i-3])
                nums[i] += nums[i-2];
            else
                nums[i] += nums[i-3];
        }
        return max(nums[len-1], nums[len-2]);
    }

Log in to reply
 

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