Best Time to Buy and Sell Stock III
Given an array prices where prices[i] is the price of a stock on day i, find the maximum profit you can achieve completing at most two transactions. You may not hold more than one share at a time and must sell before buying again.
Open official problem prompt ↗Maximize profit when at most two complete buy-sell round trips are allowed.
Like a relay with exactly two legs: the money you finish the first leg with becomes the capital you carry into the second, so each stage builds on the one before it.
- Input
- prices = [3, 3, 5, 0, 0, 3, 1, 4]
- Output
- 6
- Why
- Buy at 0 (day 4) sell at 3 (day 6) is +3, then buy at 1 (day 7) sell at 4 (day 8) is +3; total 6.
1 <= prices.length <= 10^50 <= prices[i] <= 10^5