首尾相连
s1 抢0到n-2
s2 抢1到n-1
class Solution {
public:
int rob(vector<int>& nums) {
int n=nums.size();
if(n==0) return 0;
if(n==1) return nums[0];
if(n==2) return max(nums[0],nums[1]);
vector<int> s1(n);
vector<int> s2(n);
s1[0]=nums[0];
s2[n-1]=nums[n-1];
s1[1]=max(nums[1],s1[0]);
s2[n-2]=max(nums[n-2],s2[n-1]);
//s1 0 n-2
//s2 1 n-1
int i;
for(i=2;i<n-1;i++){
s1[i]=max(nums[i]+s1[i-2],s1[i-1]);
}
s1[i]=s1[i-1];
for(i=n-3;i>0;i--){
s2[i]=max(nums[i]+s2[i+2],s2[i+1]);
}
s2[i]=s2[i+1];
return max(s1[n-1],s2[0]);
}
};