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

## Problem Statement

\


You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` 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.

&#x20;

**Example 1:**

<pre><code><strong>Input: prices = [1,3,2,8,4,9], fee = 2
</strong><strong>Output: 8
</strong><strong>Explanation: The maximum profit can be achieved by:
</strong>- 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.
</code></pre>

**Example 2:**

<pre><code><strong>Input: prices = [1,3,7,5,10,3], fee = 3
</strong><strong>Output: 6
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= prices.length <= 5 * 104`
* `1 <= prices[i] < 5 * 104`
* `0 <= fee < 5 * 104`\
  \


## Intuition

```
Just subtract transaction fee from the II question
```

### Links

[https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)

### Video Links

[https://www.youtube.com/watch?v=k4eK-vEmnKg\&ab\_channel=takeUforward](https://www.youtube.com/watch?v=k4eK-vEmnKg\&ab_channel=takeUforward)

### Approach 1:

```
MEmoization
```

{% code title="C++" lineNumbers="true" %}
```cpp
class Solution {
public:
    int find_ans(vector<int>& prices, int index, bool buy, vector<vector<int>> &dp, int fee){

        if(index == prices.size())
            return 0;

        if(dp[index][buy] != -1)
            return dp[index][buy];

        int profit;

        if(buy){
            profit = max ( -prices[index] + find_ans(prices, index+1, false, dp, fee), 
                                find_ans(prices, index+1, true, dp, fee) );
            /*
                If buy, 
                    two cases, buy/dont buy change flag accordingly
                    
                 else if sell
                    two cases, Sell/Not sell 
            */
        }

        else{
            profit = max( prices[index] - fee + find_ans(prices, index+1, true, dp, fee) ,
                                    find_ans(prices, index+1, false, dp, fee) );
        }

        return dp[index][buy] = profit;
    }

    int maxProfit(vector<int>& prices, int fee) {
        int n = prices.size();
        vector<vector<int>> dp (n, vector<int>(2,-1));

        return find_ans(prices, 0, true, dp, fee);
    }
};
```
{% endcode %}

### Approach 2:

```
Tabulation    
```

{% code title="C++" lineNumbers="true" %}
```cpp
class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        int n = prices.size();
        vector<vector<int>> dp (n+1, vector<int>(2,0));

        //Base Case, No need here just added
        dp[n][0] = dp[n][1] = 0;

        for(int index=n-1; index>=0; index--){
            for(int buy=0; buy<=1; buy++){
                int profit;

                if(buy)
                    profit = max ( -prices[index] + dp[index+1][0], dp[index+1][1] );
                
                else
                    profit = max( prices[index] - fee + dp[index+1][1] ,dp[index+1][0] );

                dp[index][buy] = profit;
            }
        }

        return dp[0][1];
    }
};
```
{% endcode %}

### Approach 3:

```
Space Optimization
```

{% code title="C++" lineNumbers="true" %}
```cpp
class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        int n = prices.size();
        vector<int> prev(2,0), cur(2,0);

        //Base Case, No need here just added
        cur[0] = cur[1] = 0;

        for(int index=n-1; index>=0; index--){
            for(int buy=0; buy<=1; buy++){
                int profit;

                if(buy)
                    profit = max ( -prices[index] + cur[0], cur[1] );
                
                else
                    profit = max( prices[index] - fee + cur[1] ,cur[0] );

                prev[buy] = profit;
            }

            cur = prev;
        }

        return prev[1];
    }
};
```
{% endcode %}

### Approach 4:

{% code title="C++" lineNumbers="true" %}
```cpp
```
{% endcode %}

### Similar Problems

###

