class Solution {
public:
int maxProfit(vector<int>& prices){
int n=prices.size();
vector<int>un(n+1,0);
vector<int>ho(n+1,0);
if(n<2) return 0;
ho[1]=-prices[0];
for(int i=2;i<=n;++i)
{
ho[i]=max(ho[i-1],un[i-2]-prices[i-1]);
un[i]=max(ho[i]+prices[i-1],un[i-1]);
}
return max(ho[n]+prices[n-1],un[n-1]);
}
};
leetcode - 309. Best Time to Buy and Sell Stock with Cooldown
最新推荐文章于 2024-08-12 07:30:03 发布