Best Time to Buy and Sell Stock II
With unlimited trades, collect every day-to-day price rise. A simple greedy idea that is provably best.
01The problem
Same prices as before, but now you may trade as many times as you like (holding at most one share at a time). Return the biggest total profit.
| Input | Output | Why |
|---|---|---|
[7, 1, 5, 3, 6, 4] | 7 | buy 1 sell 5 (+4), buy 3 sell 6 (+3) |
[1, 2, 3, 4, 5] | 4 | buy 1, sell 5 |
[7, 6, 4, 3, 1] | 0 | no rises |
Loading the rest of the lesson…