congr / world

2 stars 1 forks source link

LeetCode : 198. House Robber #485

Closed congr closed 5 years ago

congr commented 5 years ago

https://leetcode.com/problems/house-robber/

image

congr commented 5 years ago

class Solution { public int rob(int[] nums) { if (nums.length == 0) return 0; if (nums.length == 1) return nums[0];

    int[] D = new int[nums.length];

    D[0] = nums[0];
    D[1] = Math.max(nums[0], nums[1]);
    for(int i = 2; i < nums.length; i++) 
        D[i] = Math.max(D[i-2] + nums[i], D[i-1]);

    return D[nums.length-1];
}

}