rates[i] is its price on day i. You may buy the coin once and sell it once on a later day.
Return the largest profit (selling price minus buying price) you can make. If no profit is possible, return 0.rates=[12, 15, 4, 9, 7, 11] → 7
The best move is to buy on the day the rate is 4 and sell when it is 11: 11 − 4 = 7. Buying at 12 and selling at 15 earns only 3.
rates=[9, 9, 5, 2] → 0
The rate only falls or stays flat, so no profit is possible and the answer is 0.
rates=[3] → 0
With a single day you cannot buy and sell on different days.
rates.length ≤ 10⁵rates[i] ≤ 10⁴rates=[12, 15, 4, 9, 7, 11]
7
Run checks the first 3 tests in your browser. Submit checks all 102 tests on the server, hidden ones included.