Skip to main content
Medium Dynamic Programming Medium frequency

Best Time to Buy and Sell Stock with Transaction Fee

Open on LeetCode

Approach Summary

State machine: cash (not holding) and hold (holding). cash = max(cash, hold + price - fee); hold = max(hold, cash - price). Unlimited transactions.

How to Recognize This Pattern

  • Unlimited transactions with per-trade fee
  • State machine DP: two states, two transitions

Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(1)

Tags

Array Dynamic Programming Greedy

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee