121. Best Time to Buy and Sell Stock
# Easy

Solution:
if prices[i]<minpeak, 找到了当前最小值
if prices[i] 不是最小值,计算profit,看能否获得最大盈利
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# edge case
if len(prices) == 0:
return 0
maxprofit = 0
minpeak = prices[0]
for i in range(len(prices)):
if prices[i] < minpeak:
minpeak = prices[i]
elif prices[i] - minpeak > maxprofit:
maxprofit = prices[i] - minpeak
return maxprofit
Time complexity = , space complexity =
Previous117. Populating Next Right Pointers in Each Node IINext122. Best Time to Buy and Sell Stock II
Last updated
Was this helpful?