动态规划_leetcode122(股票交易,双状态,状态好难定义)

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

if not prices:
return 0


buy = [0 for i in range(len(prices))]

sell = [0 for i in range(len(prices))]

buy[0] = -prices[0]


for i in range(1,len(prices)):
# 状态好难定义
# 考虑第i天买股票时的最大收益
buy[i] = max(buy[i-1],sell[i-1]-prices[i])
# 考虑第i天买股票的最大收益
sell[i] = max(sell[i-1],buy[i-1] + prices[i])

return sell[-1]
posted @ 2019-03-17 12:52  AceKo  阅读(377)  评论(0)    收藏  举报