题目描述
题目链接
https://leetcode.com/problems/house-robber-ii/
方法思路
class Solution {
//Runtime: 0 ms, faster than 100.00%
//Memory Usage: 35.5 MB, less than 94.44%
public int rob(int[] nums) {
if (nums.length == 1) return nums[0];
int include_Head = helper(nums, 0, nums.length - 2);
int exclude_Head = helper(nums, 1, nums.length - 1);
return Math.max(include_Head, exclude_Head);
}
private int helper(int[] nums, int head, int tail){
if(tail < head) return 0;
if(tail == head) return nums[tail];
int a = nums[head];
int b = Math.max(a, nums[head + 1]);
for(int i = head + 2; i <= tail; i++) {
int A = a + nums[i];
int B = b;
int max = Math.max(A, B);
a = b;
b = max;
}
return b;
}
}