prices holds a stock's price day by day: prices[i] is the price on day i.
Pick two days i < j with prices[i] < prices[j] so that prices[j] - prices[i] is as large as possible, and return that difference. If the price never climbs above any earlier price, return -1.prices=[6, 2, 9, 4, 11] → 9
Day 1 (price 2) and day 4 (price 11) give a rise of 9. The pair 6 → 11 gives only 5.
prices=[8, 8, 5, 3] → -1
The price never goes up, and equal prices do not count as a rise. The answer is -1.
prices=[10, 1, 3] → 2
10 is above every later price, so only the pair 1 → 3 works: a rise of 2.
prices=[6, 2, 9, 4, 11]
9
Run checks the first 3 tests in your browser. Submit checks all 116 tests on the server, hidden ones included.