121. Best Time to Buy and Sell Stock
Given an array of daily prices, find the maximum profit from buying on one day and selling on a later day (or 0 if no profit is possible).
def max_profit(prices):
min_price = prices[0]
best = 0
for p in prices[1:]:
best = max(best, p - min_price)
min_price = min(min_price, p)
return best
The 'window' here is implicit: the left edge is the cheapest price seen so far, the right edge is today. One pass keeps both the running minimum and the best profit against it updated together.