prices[i] is a stock's price on day i. You may make at most two trades, where a trade means buying one share and selling it on a later day. You can hold only one share at a time, so the first trade must be sold before the second one is bought. Selling and buying again on the same day is allowed. Return the largest total profit you can make, or 0 if no trade makes money.
Buy at 1 and sell at 8 (+7), then buy at 2 and sell at 7 (+5). Three trades could earn 15 here (1 → 6, 3 → 8, 2 → 7), but only two are allowed, and the best pair of them makes 10.
The price only climbs, so one trade from day 0 to day 3 is best. A second trade can't add anything.
The price never rises, so the best plan is not to trade.
Constraints
1 <= prices.length <= 10⁵0 <= prices[i] <= 10⁵Two trades never overlap in time, so some day splits them: the first trade is finished by that day and the second starts on or after it. Each half is then the one-trade problem.
12