Best Time to Buy and Sell Stock III - Problem

You're a stock trader with a special constraint: you can only make at most two transactions (buy-sell pairs) to maximize your profit.

Given an array prices where prices[i] represents the stock price on day i, find the maximum profit you can achieve with at most 2 complete transactions.

Important Rules:

  • You must sell before buying again (no simultaneous transactions)
  • A transaction consists of buying and then selling one share
  • You can choose to make 0, 1, or 2 transactions

Example: If prices = [3,3,5,0,0,3,1,4], you can buy at 3, sell at 5 (profit=2), then buy at 0, sell at 4 (profit=4) for total profit of 6.

Input & Output

example_1.py โ€” Basic Case
$ Input: prices = [3,3,5,0,0,3,1,4]
โ€บ Output: 6
๐Ÿ’ก Note: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3. Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 3. Total profit = 6.
example_2.py โ€” Single Transaction Better
$ Input: prices = [1,2,3,4,5]
โ€บ Output: 4
๐Ÿ’ก Note: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 4. Making a second transaction would not increase profit.
example_3.py โ€” No Profit Possible
$ Input: prices = [7,6,4,3,1]
โ€บ Output: 0
๐Ÿ’ก Note: Prices only decrease, so no profitable transactions are possible.

Visualization

Tap to expand
Trading State TransitionsHOLD CASHBUY1: -priceHOLD STOCK 1SELL1: +priceHOLD CASH 2BUY2: -priceHOLD STOCK 2SELL2: +priceExample: prices = [3,3,5,0,0,3,1,4]Day 0 (price=3): buy1=-3, sell1=0, buy2=-3, sell2=0Day 1 (price=3): buy1=-3, sell1=0, buy2=-3, sell2=0Day 2 (price=5): buy1=-3, sell1=2, buy2=-3, sell2=2Day 3 (price=0): buy1=-3, sell1=2, buy2=2, sell2=2Day 4 (price=0): buy1=-3, sell1=2, buy2=2, sell2=2Day 5 (price=3): buy1=-3, sell1=2, buy2=2, sell2=5Day 6 (price=1): buy1=-3, sell1=2, buy2=2, sell2=5Day 7 (price=4): buy1=-3, sell1=2, buy2=2, sell2=6
Understanding the Visualization
1
Initialize States
Start with four states tracking maximum profit at each trading stage
2
Process Each Day
For each price, decide whether to transition states or maintain current position
3
State Updates
Update each state by choosing maximum between staying or taking action
4
Extract Result
Final result is in sell2 state, representing maximum profit from at most 2 complete transactions
Key Takeaway
๐ŸŽฏ Key Insight: We don't need to track all possible transaction combinations - just maintain the optimal profit for each trading state and transition between them optimally.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array, updating four states at each step

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using four variables to track the states

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค prices.length โ‰ค 105
  • 0 โ‰ค prices[i] โ‰ค 105
  • At most 2 transactions allowed
Asked in
Amazon 85 Google 72 Microsoft 64 Meta 51 Apple 43
68.3K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen