动态规划_leetcode121(好难,双状态)

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

if not prices:
return 0


buy = -prices[0]

sell = 0


for i in range(1,len(prices)):
buy = max(buy ,-prices[i])
sell = max (sell,prices[i] + buy)

return sell
原文地址:https://www.cnblogs.com/lux-ace/p/10546508.html