class Solution {
public:
int maxProfit(vector<int>& prices) {
vector<vector<int>> maxProfits(prices.size(),vector<int>(2,0));
int maxPos = prices.size() - 1;
if(maxPos <= 0 ) return 0;
maxProfits[0][0] = 0;
maxProfits[0][1] = prices[0];//top
maxProfits[0][2] = prices[0];//bottom
for(int i=1;i<=maxPos;i++){
//maxProfits[i][0] = max(maxProfits[i-1][0],max(maxProfits[i-1][1]-prices[i],prices[i] - maxProfits[i-1][2]));
maxProfits[i][0] = max(maxProfits[i-1][0],prices[i] - maxProfits[i-1][2]);
maxProfits[i][2] = prices[i]<maxProfits[i-1][2] ? prices[i] : maxProfits[i-1][2];
}
return maxProfits[maxPos][0];
}
};
public:
int maxProfit(vector<int>& prices) {
vector<vector<int>> maxProfits(prices.size(),vector<int>(2,0));
int maxPos = prices.size() - 1;
if(maxPos <= 0 ) return 0;
maxProfits[0][0] = 0;
maxProfits[0][1] = prices[0];//top
maxProfits[0][2] = prices[0];//bottom
for(int i=1;i<=maxPos;i++){
//maxProfits[i][0] = max(maxProfits[i-1][0],max(maxProfits[i-1][1]-prices[i],prices[i] - maxProfits[i-1][2]));
maxProfits[i][0] = max(maxProfits[i-1][0],prices[i] - maxProfits[i-1][2]);
maxProfits[i][2] = prices[i]<maxProfits[i-1][2] ? prices[i] : maxProfits[i-1][2];
}
return maxProfits[maxPos][0];
}
};