> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Best Time to Buy and Sell Stock with

> Tested Python solution for LeetCode 714 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 714, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), [Greedy](/catalog/topics/greedy). [View on LeetCode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 714   # by problem number
lcpy gen -s best_time_to_buy_and_sell_stock_with_transaction_fee   # by problem name
```

## Problem

You are given an array `prices` where `prices[i]` is the price of a given stock on the `i^th` day, and an integer `fee` representing a transaction fee.

Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.

**Note:**

* You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
* The transaction fee is only charged once for each stock purchase and sale.

### Examples

```
Input: prices = [1,3,2,8,4,9], fee = 2
Output: 8
```

**Explanation:** The maximum profit can be achieved by:

* Buying at prices\[0] = 1
* Selling at prices\[3] = 8
* Buying at prices\[4] = 4
* Selling at prices\[5] = 9
  The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

```
Input: prices = [1,3,7,5,10,3], fee = 3
Output: 6
```

### Constraints

* 1 \<= prices.length \<= 5 \* 10^4
* 1 \<= prices\[i] \< 5 \* 10^4
* 0 \<= fee \< 5 \* 10^4

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/best_time_to_buy_and_sell_stock_with_transaction_fee/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/best_time_to_buy_and_sell_stock_with_transaction_fee/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def max_profit(self, prices: list[int], fee: int) -> int:
        cash = 0
        hold = -prices[0]
        for price in prices[1:]:
            cash = max(cash, hold + price - fee)
            hold = max(hold, cash - price)
        return cash
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.