FunctionRacerProblemsRace

Best Time to Trade Stock Many Times

EasyGreedyPython, JavaScript, TypeScript, Java, C++

Item prices[i] of the list prices is a stock 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 (sell before buying again; selling and buying on the same day is fine).

Return the biggest total profit.

Example 1

prices [7,1,5,3,6,4]
returns 7

Buy at 1, sell at 5 (+4); buy at 3, sell at 6 (+3): 7.

Example 2

prices [5,4,3]
returns 0

Prices only fall, so do nothing: 0.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Greedy problems