Best Time to Buy and Sell Stock III
HardP3Coding
prices[i] is a stock price on day i. You may complete at most two transactions (a transaction is one buy followed by one sell), and you must sell before you buy again, so you never hold more than one share.
Return the maximum profit. If no profit is possible, return 0.
Example 1
- Input
- prices = [3,3,5,0,0,3,1,4]
- Output
- 6
- Why
- Buy at 0, sell at 3; buy at 1, sell at 4: profit 3 + 3 = 6.
Example 2
- Input
- prices = [1,2,3,4,5]
- Output
- 4
- Why
- One transaction from 1 to 5 earns 4, and two do not beat it.
Example 3
- Input
- prices = [7,6,4,3,1]
- Output
- 0
Constraints
- 1 <= prices.length <= 10^5
- 0 <= prices[i] <= 10^5
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.