![](https://img-blog.csdnimg.cn/direct/927776ceabc2496aab21b3a90df47d30.png)
class Solution {
public:
int massage(vector<int>& nums) {
int size = (int)nums.size();
if(!size)return 0;
if(size==1)return nums[0];
if(size<3)return max(nums[0],nums[1]);
int* dp= new int[size];
int i = 3;
dp[0]=nums[0];
dp[1]=nums[1];
dp[2]=dp[0]+nums[2];
for(;i<size;++i){
dp[i]=max(dp[i-3],dp[i-2])+nums[i];
}
return max(dp[i-1],dp[i-2]);
}
};
// 节约空间可以这么写,不过提交后没有明显的优化
class Solution {
public:
int massage(vector<int>& nums) {
char size = nums.size();
if(!size)return 0;
else if(size<3)return max(nums[0],nums[size-1]);
char i = 3;
nums[2]+=nums[0];
for(;i<size;++i){
char j = i-3;
nums[i]+=max(nums[j],nums[j+1]);
}
return nums[i-1]>nums[i-2]?nums[i-1]:nums[i-2];
}
};
![](https://img-blog.csdnimg.cn/direct/6b61fa36bbae404a8f2f512366f52a11.png)