Sliding WindowEasy

Best Time to Buy and Sell Stock

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.

Example 1prices = [8, 2, 6, 1, 4] → 4Buy at 2, sell at 6.
Example 2prices = [9, 7, 3] → 0
1 ≤ n ≤ 10⁵ · 0 ≤ prices[i] ≤ 10⁴

Hints

0 of 3 open
Hint 2 · the patternHint 3 · the key line