Quant Memo
Coding/●●●●●

Max profit with unlimited trades

You are given the intraday price path of a stock. Now you may complete as many trades as you want, but you can hold at most one share at a time, so you must sell before buying again. Short selling is not allowed.

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

prices = [7, 6, 4, 3, 1]
-> 0           # only falls; skip every trade

Return the maximum total profit. Aim for O(n)O(n) time and O(1)O(1) space.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions