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

## Problem Statement

<br>

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`\ <br>

## 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/>

### Video Links

<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

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/dynamic-programming/dp-on-stocks/714.-best-time-to-buy-and-sell-stock-with-transaction-fee.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
