# Middle-题目19：309. Best Time to Buy and Sell Stock with Cooldown

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

Middle-题目4相同，区别是卖出股票之后会有一天的CD（冷却期），而在CD的时候是不能买入的。求最大利益。

（2） 如果是卖出，则第i天是CD，啥也干不了，手里一定是没有股票的，这种情况无解。

（2） 如果是卖出，则第i天是cd，此时sell[i]=sell[i-1]
（3） 如果啥也没做，而手里有股票，还是sell[i]=sell[i-1]

public class Solution {
public int maxProfit(int[] prices) {
for(int price : prices) {
preSell = sell;
sell = Math.max(preBuy + price, preSell);
}
return sell;
}
}

1ms,beats 87.89%,众数2ms，40.16%
Cmershen的碎碎念：
DP问题的关键是找出转移数组和转移方程。而这道题的转移方程对我来说比较难想，应多注意。

• 本文已收录于以下专栏：

举报原因： 您举报文章：Middle-题目19：309. Best Time to Buy and Sell Stock with Cooldown 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)