# [leetcode] 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)
Example:

prices = [1, 2, 3, 0, 2]
maxProfit = 3

sell = max(last_sell, last_buy + price[i])
cooldown = max(cooldown, last_sell);

class Solution {
public:
int maxProfit(vector<int>& prices) {
int last_sell = 0, last_buy = INT_MIN, last_cooldown = 0, sell = 0, buy = 0, cooldown = 0;
for(auto price:prices) {
sell = max(last_sell, last_buy + price);
cooldown = max(last_cooldown, last_sell);
last_sell = sell;
last_cooldown = cooldown;
}
return sell;
}
};

#### [leetcode] 309. Best Time to Buy and Sell Stock with Cooldown 解题报告

2016-06-14 14:53:30

#### 309. Best Time to Buy and Sell Stock with Cooldown

2016-04-09 14:04:29

#### [leetcode 309]Best Time to Buy and Sell Stock with Cooldown--买卖股票with冷却期（DP法）

2016-03-10 23:02:33

#### leetcode 309. Best Time to Buy and Sell Stock with Cooldown 解题报告

2016-09-03 16:49:25

#### leetcode刷题，总结，记录，备忘 309

2015-12-16 21:01:32

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

2018-05-15 14:48:40

#### LeetCode 309: 一个很清晰的DP解题思路

2017-07-31 20:26:04

#### LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

2016-10-25 17:53:52

#### [leetcode]309. Best Time to Buy and Sell Stock with Cooldown

2016-04-13 23:59:36

#### leetcode 121 309

2017-12-18 12:10:23