# 309. 最佳买卖股票时机含冷冻期(leetcode/C++)

• 你不能同时参与多笔交易（你必须在再次购买前出售掉之前的股票）。
• 卖出股票后，你无法在第二天买入股票 (即冷冻期为 1 天)。

prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]

profits[j]表示在第j天的收益，dp[i][j]表示第i天买第j天卖的收益。

class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() == 0 || prices.size() == 1)
{
return 0;
}
vector<vector<int> > dp;
vector<int> profits(prices.size(), 0);
for (int i = 0; i < prices.size(); i++)
{
dp.push_back(vector<int> ());
for (int j = 0; j < prices.size(); j++)
{
dp[i].push_back(0);
}
}

for (int i = 0; i < prices.size(); i++)
{
for (int j = i + 1; j < prices.size(); j++)
{
dp[i][j] = prices[j] - prices[i];
if (dp[i][j] < 0)
{
dp[i][j] = 0;
}
}
}
profits[1] = dp[0][1];

for (int j = 1; j < prices.size(); j++)
{
if (j - 1 >= 0)
{
profits[j] = max(profits[j], profits[j - 1]);
}
if (j - 2 >= 0)
{
profits[j] = max(profits[j], profits[j - 2]);
}
for (int i = 0; i < j; i++)
{
profits[j] = max(profits[j], dp[i][j]);
if (i - 2 >= 0)
{
profits[j] = max(profits[j], dp[i][j] + profits[i - 2]);
}
}
}
return profits[prices.size() - 1];
}
};