# 213. 打家劫舍 II (low 和i的关系)

你是一个专业的小偷，计划偷窃沿街的房屋，每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈，这意味着第一个房屋和最后一个房屋是紧挨着的。同时，相邻的房屋装有相互连通的防盗系统，如果两间相邻的房屋在同一晚上被小偷闯入，系统会自动报警。

偷窃到的最高金额 = 1 + 3 = 4 。
class Solution {
public int robCycle(int[] nums,int low,int high){
int len = high - low + 1;

int s1 = 0;//not rob
int s2 = 0;//rob
for(int i=0;i<len;i++){
int tmp = s1;
s1 = Math.max(s1,s2);//not rob
s2 = nums[low+i] + tmp;//rob
}

return Math.max(s1,s2);
}
public int rob(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
if(nums.length == 1)
return nums[0];
int a = robCycle(nums,0,nums.length-2);
int b = robCycle(nums,1,nums.length-1);
return Math.max(a,b);
}
}
class Solution {
public int robCycle(int[] nums,int low,int high){
int len = high - low + 1;
if(len == 1)
return nums[low];
if(len == 2)
return Math.max(nums[low],nums[low+1]);

int[] val = new int[len];
val[0] = nums[low];
val[1] = Math.max(nums[low],nums[low+1]);

int i = 2;
while(i < len){
val[i] = Math.max(val[i-1],nums[low+i]+val[i-2]);
i++;
}
return val[i-1];
}
public int rob(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
if(nums.length == 1)
return nums[0];
int a = robCycle(nums,0,nums.length-2);
int b = robCycle(nums,1,nums.length-1);
return Math.max(a,b);
}
}
class Solution {
public int robCycle(int[] nums,int low,int high){
int[] val = new int[nums.length - 1];

val[0] = nums[low];
val[1] = Math.max(nums[low],nums[low+1]);
int i = 2;
while(low + i <= high){
val[i] = Math.max(val[i-1],nums[low+i]+val[i-2]);
i++;
}
return val[i-1];
}
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]);

int a = robCycle(nums,0,nums.length-2);
int b = robCycle(nums,1,nums.length-1);
return Math.max(a,b);
}
}