Updated November 2026

The best time to buy and sell stock III problem asks you to find the maximum profit from a daily price list when you may complete at most two buy-sell transactions. You must sell before you buy again, and you cannot hold more than one share at a time. This guide shows you how to reason through the constraint, build a working solution, and check your answer against edge cases.

Understand the Two-Transaction Constraint

Each transaction is one buy followed later by one sell. With at most two transactions, the possible patterns are: no trade, one trade, or two trades that do not overlap. The hard part is that the two trades can happen at any two points, so you cannot simply pick the lowest and highest prices in the list.

Shopping for one? See our guide: 5 Best Time to Buy Washer and Dryers: Compact Laundry Picks for 2026.

A useful way to think about it is to split the timeline at some day k. The first trade happens entirely before k, and the second happens entirely after. If you can compute the best single-trade profit on each side, the answer is the best total across all split points.

Use a State-Based Dynamic Programming Method

The cleaner approach tracks four states as you scan prices once from left to right. Each state stores the best cash balance reachable at that point:

  1. buy1: the best balance after the first buy (initialize to negative infinity).
  2. sell1: the best balance after the first sell (initialize to 0).
  3. buy2: the best balance after the second buy, counting the first sale’s profit (initialize to negative infinity).
  4. sell2: the best balance after the second sell (initialize to 0).

For each price p, update the states in this order:

  • buy1 = max(buy1, -p)
  • sell1 = max(sell1, buy1 + p)
  • buy2 = max(buy2, sell1 – p)
  • sell2 = max(sell2, buy2 + p)

The answer is sell2. This runs in O(n) time with O(1) extra space, which is why interviewers and competitive programmers usually expect it.

Trace a Small Example

Take the prices 3, 3, 5, 0, 0, 3, 1, 4. Scanning through the states, the first trade buys at 0 and sells at 3 for a profit of 3. The second buys at 1 and sells at 4 for a profit of 3. The total is 6, and sell2 ends at that value.

Check Edge Cases and Verify Your Result

Before trusting any implementation, test these inputs:

Input pattern Expected behavior
Prices never rise (strictly decreasing) Result is 0; no trade is profitable.
Single day or empty list Result is 0.
One long upward run One trade captures the full rise; a second trade adds nothing extra.
Two separate rises with a dip between Two trades capture both rises.

Common Mistakes

  • Allowing a second buy on the same day as the first sell without accounting for the cash from that sale.
  • Initializing buy states to 0 instead of negative infinity, which silently lets a free buy into the calculation.
  • Confusing “at most two transactions” with “exactly two,” which changes whether a zero-profit result is valid.
  • Updating states in the wrong order within the same loop iteration, so a sell uses a buy from the same day.
  • Forgetting that the DP result should be checked against brute force on small random arrays.

If you practice these problems on paper or want a dedicated reference while studying, a sturdy notebook helps you trace state updates by hand. View on Amazon

A well-regarded book on algorithmic interview problems can walk you through dynamic programming patterns in more depth. View on Amazon

FAQ

Can I hold two shares at the same time?

No. The standard version of this problem allows only one share held at any moment, so you must sell before buying again.

Why use four states instead of two?

Two states can only track one transaction. Four states let you track the first buy, first sell, second buy, and second sell separately, which keeps the logic correct when trades are chained.

Is there a simpler method than dynamic programming?

You can compute the best single-trade profit for every prefix and suffix of the array and combine them at each split point. It is easier to explain but uses more memory and code than the four-state approach.

Conclusion

The best time to buy and sell stock III problem becomes manageable once you treat it as two linked single-trade problems tracked through four states. Work through the trace example, test the edge cases, and compare your output against a brute-force solution on small inputs. Once the state order is clear, the O(n) solution is short and reliable.

Owen Palmer

About Owen Palmer

Owen Palmer is part of our editorial team and reviews and edits our holiday shopping calendar guides, checking product details against manufacturer specifications and owner feedback.