# problem

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

# solution1

class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""

def helper(prices, start, dp):
if len(prices) < 2:
return 0
ans = 0
for i in range(start, len(prices)):
for j in range(i + 1, len(prices)):
if dp[j+2] != -1:
ans = max(ans, prices[j] - prices[i] + dp[j+2])
else:
ans = max(ans, prices[j] - prices[i] + helper(prices, j+2, dp))
dp[start] = ans

return ans

dp = [-1] * (len(prices)+2)
return helper(prices, 0, dp)

# solution2

sell[i] = max(buy[i-1] + price, sell[i-1])

# 总结

#### 【Python】第二周Python基础（下）

2018年03月16日 11:34

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

2016-02-22 11:40:30

#### [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-06-14 14:53:30

#### leetcode【121+122+123 best time to buy and sell stock】【python】

2016-09-12 21:04:35

#### 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

2015-04-06 11:10:34

#### 【LeetCode】【Python题解】Best Time to Buy and Sell Stock II

2014-07-24 10:11:44

#### [LeetCode] Best Time to Buy and Sell Stock IV

2015-02-23 00:08:34

#### leetcode -- Best Time to Buy and Sell Stock with Cooldown -- 不理解

2015-12-11 12:39:35

#### 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock（最佳买卖股票的时间）】

2015-08-14 06:16:32

## 不良信息举报

[python]leetcode(309). Best Time to Buy and Sell Stock with Cooldown