标签:lis etc ref fit href not ice 最小 min
121. Best Time to Buy and Sell Stock
用一个变量minprice记录当前已经遍历过的最小价格,再用一个变量maxprofit记录当前已经遍历过的最大利润,如果price[i] > minprice,就计算最大利润是否需要增加;否则计算最小价格是否需要改变。
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if not prices:
return 0
minprice = prices[0]
maxprofit = 0
for i in range(1, len(prices)):
if prices[i] > minprice:
maxprofit = max(maxprofit, prices[i] - minprice)
else:
minprice = min(minprice, prices[i])
return maxprofit
LeetCode #121. Best Time to Buy and Sell Stock
标签:lis etc ref fit href not ice 最小 min
原文地址:https://www.cnblogs.com/RatsCommander/p/13853050.html