prices[i] is a stock's price on day i. You may trade as often as you like, under two rules:
Each buy and each sale happens at that day's price. Return the largest total profit you can make. Never trading at all earns 0.
Buy at 2 and sell at 5 (+3). Day 2 is then a rest day, so the low price of 1 can't be used. Buy at 4 and sell at 7 (+3), for 6 in all. Buying at 1 and selling at 7 also earns 6. Without the rest day, 2 → 5 and then 1 → 7 would earn 9.
Buy at 1 and sell at 4 (+3), rest on day 3, then buy at 2 and sell at 9 (+7). Holding from 1 to 8 earns more on its own (+7), but the rest day after it would fall on the 2, and nothing later is worth buying.
The price only falls, so the best plan is never to buy.
Constraints
1 <= prices.length <= 50000 <= prices[i] <= 1000At the end of each day you are in one of three situations: holding a share, having sold today (so tomorrow is a rest day), or holding nothing and free to buy tomorrow. Track the best profit you can have in each.
6