class Solution {
public:
int maxProfit(vector<int>& prices) {
vector< vector<int> > dp(2, vector<int>(prices.size(), 0));
dp[0][0] = -prices[0];
dp[1][0] = 0;
for(vector<int>::size_type i = 1; i < prices.size(); i++) {
dp[0][i] = max(dp[0][i - 1], -prices[i]);
dp[1][i] = max(dp[1][i - 1], dp[0][i - 1] + prices[i]);
}
return dp[1].back();
}
};
122. 买卖股票的最佳时机 II - 力扣(Leetcode)
class Solution {
public:
int maxProfit(vector<int>& prices) {
vector< vector<int> > dp(2, vector<int>(prices.size(), 0));
dp[0][0] = -prices[0];
for(vector<int>::size_type i = 1; i < prices.size(); i++) {
dp[0][i] = max(dp[0][i - 1], dp[1][i - 1]-prices[i]);
dp[1][i] = max(dp[1][i - 1], dp[0][i - 1] + prices[i]);
}
return dp[1].back();
}
};