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]
------------------------------------------------------------------------
这题容易受到没有cooldown那个影响,总想在低点买入,高点卖出,但是卖出cooldown要放在卖那天还是买之后,不知道。。。所以应该可以想到动态规划。。股票交易的特殊思路很多,这里动态规划是一种通用的思路。。。
但是怎么动态规划呢,如果把买、卖、cooldown都作为状态,cooldown的状态可能是买入后cooldown,也可能是卖出后cooldown,跳转还有额外的限制,想起来非常晕。。
比较简化的办法是把买、卖、cooldown都作为动作,只有手里有没有股票才是状态,这就是典型的状态DP,状态DP一定要简化状态和动作。在这题里,第几天、有没有股票都是状态,更复杂一些,推广到另外几道股票题目,交易了几次也是状态。这样不难写出状态转移方程。
t[i][0] = max(t[i-1][0],t[i-1][1]+prices[i])
t[i][1] = max(t[i-1][1],t[i-2][0]-prices[i])
注意这道题的状态转移方程只和前两个状态相关,因此DP数据可以拿掉,变成O(1)的空间复杂度,O(n)的时间复杂度。以下是代码:
class Solution:
# def maxProfit(self, prices) -> int:
# l = len(prices)
# if (l <= 1):
# return 0
# t = [[0 for j in range(2)] for i in range(l)]
# t[1][0] = max(0, prices[1]-prices[0]) #bug1: forget this line
# t[0][1],t[1][1] = -prices[0],max(-prices[0],-prices[1])
# for i in range(2,l):
# t[i][0] = max(t[i-1][0],t[i-1][1]+prices[i])
# t[i][1] = max(t[i-1][1],t[i-2][0]-prices[i])
# return t[l-1][0]
def maxProfit0(self, prices) -> int:
l = len(prices)
if (l <= 1):
return 0
ti0_pre, ti0 = 0, max(0, prices[1]-prices[0])
ti1_pre, ti1 = -prices[0],max(-prices[0],-prices[1])
for i in range(2,l):
tmp = ti0
ti0 = max(ti0, ti1+prices[i])
ti1 = max(ti1, ti0_pre-prices[i])
ti0_pre = tmp
return ti0
s = Solution()
print(s.maxProfit([1,2,3,0,2]))