Python Max Profit
def max_profit(a: list[int]) -> int:
price = a[0]
profit = 0
for v in a:
price = min(price, v)
profit = max(profit, v - price)
return profit
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.