class Solution {
public:
int maxProfit(vector<int>& prices) {
vector<int> buy1(prices.size(),0);
vector<int> sell1(prices.size(),0);
vector<int> buy2(prices.size(),0);
vector<int> sell2(prices.size(),0);
buy1[0]=-prices[0];
buy2[0]=-prices[0];
for(int i=1;i<prices.size();i++)
{
buy1[i]=max(buy1[i-1],-prices[i]);
sell1[i]=max(sell1[i-1],buy1[i-1]+prices[i]);
buy2[i]=max(buy2[i-1],sell1[i-1]-prices[i]);
sell2[i]=max(sell2[i-1],buy2[i-1]+prices[i]);
}
return sell2[prices.size()-1];
}
};
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
vector<int> buy(k+1,-prices[0]);
vector<int> sell(k+1,0);
buy[0]=-prices[0];
for(int i=1;i<prices.size();i++)
{
buy[1]=max(buy[1],-prices[i]);
sell[1]=max(sell[1],buy[1]+prices[i]);
for(int j=2;j<=k;j++)
{
buy[j]=max(buy[j],sell[j-1]-prices[i]);
sell[j]=max(sell[j],buy[j]+prices[i]);
}
}
return sell[k];
}
};