Problem: 188. 买卖股票的最佳时机 IV
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public: int maxProfit(int k, vector<int>& prices) { vector<vector<int>> dp(prices.size(), vector<int>(2*k+1, 0)); for(int j=1;j<2*k;j+=2) dp[0][j] = -prices[0]; for (int i = 1; i < prices.size(); i++) { for(int j=1;j<2*k;j+=2) { dp[i][j] = max(dp[i - 1][j ], dp[i - 1][j-1] - prices[i]); dp[i][j+1] = max(dp[i - 1][j+ 1], dp[i - 1][j] + prices[i]); } } return dp[prices.size()-1][2*k]; } };
|