Best Time to Buy and Sell Stock
Find the biggest profit from one buy and one later sell by tracking the cheapest price seen so far.
01The problem
prices[i] is a stock's price on day i. Buy on one day and sell on a later day. Return the biggest profit, or 0 if no trade makes money.
| Input | Output | Why |
|---|---|---|
[7, 1, 5, 3, 6, 4] | 5 | buy at 1, sell at 6 |
[7, 6, 4, 3, 1] | 0 | price only falls |
Loading the rest of the lesson…