力扣面试题17.16 按摩师
class Solution {
public:
int massage(vector<int>& nums) {
int len = nums.size();
if (len == 0)
return 0;
if (len == 1)
return nums[0];
if (len == 2)
return max<int>(nums[0], nums[1]);
int value1 = nums[0], value2 = nums[1], temp = 0;
for (int i = 2; i < len; i++)
{
temp = value2;
value2 = max<int>(value2, value2 - nums[i - 1] + nums[i]);// 去掉前一个,加上当前的
value2 = max<int>(value2, value1 + nums[i]);
value1 = temp;
}
return value2;
}
};