题目
思路
用两个变量,分别记录最小价格和当前最大利润,不断更新。
代码
class Solution:
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
minPrice = 999999
maxProfit = 0
for i in range(len(prices)):
minPrice = min(minPrice, prices[i])
maxProfit = max(maxProfit, prices[i] - minPrice)
return maxProfit