121. Best Time to Buy and Sell Stock

Practice Best Time to Buy and Sell Stock for free on NudgeCode: Easy, Greedy pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

prices[i] is a stock's price on day i. Buy on one day and sell on a later day. Return the largest profit you can make, or 0 if no trade makes money.

Example

prices = [7,1,5,3,6,4]  ->  5   (buy at 1, sell at 6)
prices = [7,6,4,3,1]    ->  0

Constraints