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.

Example 1:

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.

Example 2:

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

Constraints:

  • 1 <= prices.length <= 5 * 104

  • 1 <= prices[i] < 5 * 104

  • 0 <= fee < 5 * 104

Intuition

Just subtract transaction fee from the II question

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

https://www.youtube.com/watch?v=k4eK-vEmnKg&ab_channel=takeUforward

Approach 1:

MEmoization
C++
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);
    }
};

Approach 2:

Tabulation    
C++
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];
    }
};

Approach 3:

Space Optimization
C++
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];
    }
};

Approach 4:

C++

Similar Problems

Last updated