LeetCode121 买卖股票的最佳时机(贪心)
维护当前股票价格最小值
class Solution:
def maxProfit(self, prices: List[int]) -> int:
ans, cur_min, l = 0, prices[0], len(prices)
for i in range(1, l):
cur_min = min(cur_min, prices[i])
ans = max(ans, prices[i] - cur_min)
return ans