121.买卖股票的最佳时期
class Solution(object):
def maxProfit(self
, prices
):
"""
:type prices: List[int]
:rtype: int
"""
if not prices
:
return 0
else:
minvalue
= prices
[0]
maxpro
= 0
for i
in prices
[1:]:
minvalue
= min(minvalue
,i
)
maxpro
= max(maxpro
,i
-minvalue
)
return maxpro
因为是最简单题没有其他约束所以遍历就可以但是这种遍历的方法其实也是基于dp的思想只需要记录minvalue,不用管这个minvalue是不是在取得最大值的卖出日期之后。因为每次是比较当前的maxpro 和 当前位置-minprice,所以卖出日期一定是在最低价之前。
转载请注明原文地址: https://lol.8miu.com/read-9946.html