> For the complete documentation index, see [llms.txt](https://garylai.gitbook.io/algorithm-and-data-structure/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://garylai.gitbook.io/algorithm-and-data-structure/classic-problems/best-time-to-buy-and-sell-stock/best-time-to-buy-and-sell-stock-with-transaction-fee.md).

# 714. Best Time to Buy and Sell Stock with Transaction Fee

[714. Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)

```python
class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        if len(prices) < 2:
            return 0

        buys = [float('-inf')] * len(prices) 
        sells = [0] * len(prices) 

        for i in range(len(prices)):
            if i == 0:
                buys[i] = max(buys[i], 0 - prices[i] - fee)
            else:
                buys[i] = max(buys[i-1], sells[i-1] - prices[i] - fee)
            sells[i] = max(sells[i-1], buys[i-1] + prices[i])
        return sells[-1]
```
