Closed congr closed 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];
}
}
https://leetcode.com/problems/house-robber/