/**
* 转化为
* Max(rob(nums[0:n-1]), rob(nums[1:n]))
*/
public int rob(int[] nums) {
if (nums.length == 0) {
return 0;
}
if (nums.length == 1) {
return nums[0];
}
int[] pre = Arrays.copyOfRange(nums, 0, nums.length-1);
int[] after = Arrays.copyOfRange(nums, 1, nums.length);
return Math.max(subRob(pre), subRob(after));
}
private int subRob(int[] nums) {
int[] dp = new int[nums.length + 2];
for (int i = 0; i < nums.length; i++) {
dp[i + 2] = Math.max(dp[i] + nums[i], dp[i + 1]);
}
return dp[nums.length + 1];
}