Overview
Without a fee, the optimal strategy is to capture every single price increase. With a fee, small fluctuations might cost more in fees than they yield in profit.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Maximize profit with unlimited transactions, but every time you sell the stock, you must pay a fixed transaction fee.
Without a fee, the optimal strategy is to capture every single price increase. With a fee, small fluctuations might cost more in fees than they yield in profit.
We only need two states: `Buy` (Holding a stock) and `Sell` (Cash in hand / Empty). There are no cooldowns.
You can subtract the fee when you buy (`prevSell - prices[i] - fee`) OR when you sell (`prevBuy + prices[i] - fee`). Mathematically, both give the exact same max profit.
Since today's states only depend on yesterday's states, we don't need arrays. Two variables (`buy` and `sell`) are enough, yielding O(1) space.