Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the State-machine DP invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Represent each step with a small set of allowed states and transitions between them. Learn its decision rule, maintained state, Python cost model, and transfer from a focused drill to a full Interview Problem.
Recognize it when
Consider State-machine DP when the prompt's constraints and required operations match this shape: Represent each step with a small set of allowed states and transitions between them.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
State-machine DP represents each step with a small set of modes such as holding, sold, and resting. Each value means the best result among histories ending in exactly that mode.
List which prior modes may lead to each next mode and what action cost or reward applies. Illegal transitions must not enter the recurrence through a convenient default value.
def stock_state_trace(prices):
rest=0;hold=float("-inf");sold=float("-inf");trace=[]
for price in prices:
previous_rest,previous_hold,previous_sold=rest,hold,sold
rest=max(previous_rest,previous_sold);hold=max(previous_hold,previous_rest-price);sold=previous_hold+price
trace.append([rest,hold,sold])
return traceImplement stock_state_trace(prices). Return [rest,hold,sold] after each price for one-share trading with one-day cooldown.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Compute all next values from saved previous values. Updating one variable in place and then using it for another transition can accidentally perform two actions in the same step.
def max_cooldown_profit(prices):
rest=0;hold=float("-inf");sold=float("-inf")
for price in prices:
previous_rest,previous_hold,previous_sold=rest,hold,sold
rest=max(previous_rest,previous_sold);hold=max(previous_hold,previous_rest-price);sold=previous_hold+price
return max(rest,sold)Implement max_cooldown_profit(prices). Hold at most one share and wait one day after selling before buying.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
A full table is unnecessary when every transition depends only on the previous step. Keep one value per mode, but preserve impossible states with negative infinity rather than treating them as zero profit.
Say: “These modes are mutually exclusive and complete. Each recurrence uses only legal previous modes, and I update simultaneously.” State initial reachable states, final acceptable states, and O(n × states × transitions) time.
Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the State-machine DP invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| State-machine DP decision loop | O(n) | O(n) | Maintain the best realized cash and best wealth while holding one share, updating them simultaneously for each price. |
O(1) for the focused Track Stock State with a Fee implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Applying the pattern without proving its ordering, monotonicity, or window invariant can silently miss valid candidates.
Prevent it: Write the decision rule beside the loop and verify it against Track Stock State with a Fee before optimizing.
Avoid
def max_profit_with_fee(prices, fee):
passUse instead
def max_profit_with_fee(prices, fee):
if not prices:
return 0
cash = 0
hold = -prices[0]
for price in prices[1:]:
next_cash = max(cash, hold + price - fee)
next_hold = max(hold, cash - price)
cash, hold = next_cash, next_hold
return cashWhere you will hit this: Track Stock State with a Fee(opens in a new tab)
Charges the fee on buy and sell, reducing every profitable transaction twice.
Prevent it: Use the public tests and preserve this state: Each state represents the best value under exactly one end condition.
Avoid
def max_profit_with_fee(prices,fee):
if not prices:return 0
cash=0; hold=-prices[0]-fee
for p in prices[1:]:
cash=max(cash,hold+p-fee); hold=max(hold,cash-p-fee)
return cashUse instead
def max_profit_with_fee(prices, fee):
if not prices:
return 0
cash = 0
hold = -prices[0]
for price in prices[1:]:
next_cash = max(cash, hold + price - fee)
next_hold = max(hold, cash - price)
cash, hold = next_cash, next_hold
return cashWhere you will hit this: Track Stock State with a Fee(opens in a new tab)
Python slicing, copying, membership in lists, or repeated sorting inside the loop can invalidate the intended complexity.
Prevent it: Count every slice, copy, sort, membership check, and container update before claiming O(n).
Avoid
def max_profit_with_fee(prices,fee):
if not prices:return 0
cash=0; hold=-prices[0]-fee
for p in prices[1:]:
cash=max(cash,hold+p-fee); hold=max(hold,cash-p-fee)
return cashUse instead
def max_profit_with_fee(prices, fee):
if not prices:
return 0
cash = 0
hold = -prices[0]
for price in prices[1:]:
next_cash = max(cash, hold + price - fee)
next_hold = max(hold, cash - price)
cash, hold = next_cash, next_hold
return cashWhere you will hit this: House Robber(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27
leetcode · checked 2026-07-12