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

**<font color=red>难度: Medium</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/

> 内容描述

```

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:

Input: [1,2,3,0,2]
Output: 3 
Explanation: transactions = [buy, sell, cooldown, buy, sell]
```

## 解题方案

> 思路 1
******- 时间复杂度: O(N^2)******- 空间复杂度: O(1)******


对于今天过后，有以下三种状态

1. 没有股票 notown
  - 要么是昨天我就没有股票，然后今天我啥也没做
  - 要么是昨天cooldown，今天我啥也没做
2. 有股票 own
  - 要么是昨天就有股票了，今天没卖
  - 要么是昨天没有股票，今天买了股票
  - 要注意，昨天如果是cooldown，今天无论如何都不可能有股票
3. cool
  - 我昨天有股票，今天卖了
  
  
最后一天我的状态肯定是notown 或者 cool

beats 97.77%

```python
class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        notown = 0
        own = cool = -sys.maxsize
        for p in prices:
            notown, own, cool = max(notown, cool), max(own, notown - p), own + p
        return max(notown, cool)
```






































