I modified the code from @yulingtianxia a little for better understanding.

Basically, in the k-th iteration:

buy[k][i] represents the maximum profit if you buy the stock on day i after at most k - 1 transactions.

buy[k][i] = max(buy[k - 1][i], sell[k - 1][i - 1] - prices[i])

sell[k][i] represents the maximum profit if you sell the stock on day i after at most k - 1 transactions.

sell[k][i] = max(sell[k - 1][i], buy[k - 1][i - 1] + prices[i])

we can reuse the buy[i - 1] and sell[i - 1] as buy[i] and sell[i].

The code:

class Solution {
public:
int maxProfit(int k, vector<int> &prices) {
int N = prices.size();
if (k == 0 || N < 2) return 0;
if (k > N / 2) {
int sum = 0;
for (int i = 1; i < N; i++){
if (prices[i] > prices[i - 1]){
sum += prices[i] - prices[i - 1];
}
}
return sum;
}
vector<int> buy(k + 1, INT_MIN);
vector<int> sell(k + 1, 0);
for (int i = 0; i< N; ++i) {
for (int j = 1; 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];
}
};