Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
public int rob(int[] nums) {
if(nums==null || nums.length==0) return 0;
if(nums.length==1) return nums[0];
if(nums.length==2) return Math.max(nums[0], nums[1]);
return Math.max(helper(nums, 0, nums.length-2), helper(nums, 1, nums.length-1));
}
public int helper(int[] nums,int start,int end){
int n = end-start+1;
if(n==0) return 0;
if(n==1) return nums[start];
int[] flag = new int[n];
flag[0] = nums[start];
flag[1] = Math.max(nums[start], nums[start+1]);
for(int i=start+2;i<=end;i++){
flag[i-start] = Math.max(flag[i-start-2]+nums[i],flag[i-start-1]);
}
return flag[n-1];
}