buy[0][i] = INT_MIN / 2;
sell[0][i] = INT_MIN / 2;
buy[i][0] = std::max(buy[i - 1][0], sell[i - 1][0] - prices[i]);
*std::max_element(sell[size - 1].begin(), sell[size - 1].end());
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int size = prices.size();
if (0 == size) {
return 0;
}
k = std::min(k, size / 2);
std::vector<std::vector<int>> buy(size, std::vector<int>(k + 1));
std::vector<std::vector<int>> sell(size, std::vector<int>(k + 1));
buy[0][0] = -prices[0];
sell[0][0] = 0;
for (int i = 1; i <= k; ++i) {
buy[0][i] = INT_MIN / 2;
sell[0][i] = INT_MIN / 2;
}
for (int i = 1; i < size; ++i) {
buy[i][0] = std::max(buy[i - 1][0], sell[i - 1][0] - prices[i]);
for (int j = 1; j <= k; ++j) {
buy[i][j] = std::max(buy[i - 1][j], sell[i - 1][j] - prices[i]);
sell[i][j] = std::max(sell[i - 1][j], buy[i - 1][j - 1] + prices[i]);
}
}
return *std::max_element(sell[size - 1].begin(), sell[size - 1].end());
}
};