122. Best Time to Buy and Sell Stock II

Practice Best Time to Buy and Sell Stock II for free on NudgeCode: Medium, 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. You may buy and sell as many times as you like, but you can hold at most one share at a time (you may sell and buy again on the same day). Return the largest total profit.

Example

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

Constraints