Track the minimum price seen so far and the best profit if selling today.
Scan prices once. Maintain min_price. For each price p, update best = max(best, p - min_price).
For any sell day, the optimal buy day is the cheapest price before it; scanning maintains that invariant.
Track the minimum price seen so far and the best profit if selling today.
Scan prices once. Maintain min_price. For each price p, update best = max(best, p - min_price).
For any sell day, the optimal buy day is the cheapest price before it; scanning maintains that invariant.