188. 买卖股票的最佳时机 IV - 力扣(LeetCode)
class Solution {
public:int maxProfit(int k, vector<int>& prices) {if(prices.size()==0) return 0;vector<vector<int>>dp(prices.size(),vector<int>(2*k+1,0));for(int i=1;i<2*k;i+=2){dp[0][i]=-prices[0];}for(int i=1;i<prices.size();i++){for(int j=0;j<2*k-1;j+=2){//奇数天买入dp[i][j+1]=max(dp[i-1][j+1],dp[i-1][j]-prices[i]);//偶数天卖出dp[i][j+2]=max(dp[i-1][j+2],dp[i-1][j+1]+prices[i]);}}return dp[prices.size()-1][2*k];}
};