Poblem
Notes
此题解法完全参考他人博客。
发这篇博文作为自己的一个做题记录。
Codes
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
if(n<=1) return 0;
int pre0=0,pre1=0,pre2=0;
int s0=0,s1=-prices[0],s2=INT_MIN;
for(int i=1;i<n;++i)
{
pre0=s0;
pre1=s1;
pre2=s2;
s0=max(pre0,pre2);
s1=max(pre1,pre0-prices[i]);
s2=pre1+prices[i];
}
return max(s0,s2);
}
};