题目链接:
309. 买卖股票的最佳时机含冷冻期 - 力扣(LeetCode)
题目描述:
算法图解:
解题代码:
class Solution {
public:int maxProfit(vector<int>& prices) {int n = prices.size();vector<int> dp1(n+1);vector<int> dp2(n+1);vector<int> dp3(n+1);dp1[0] = 0-prices[0];for(int i = 1; i<n+1;i++){dp1[i] = max(dp1[i-1],dp2[i-1]-prices[i-1]);dp2[i] = max(dp2[i-1],dp3[i-1]);dp3[i] = dp1[i-1] + prices[i-1];}return max(max(dp1[n],dp2[n]),dp3[n]);}
};