Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

Best Time to Buy and Sell Stock III 🌟🌟🌟

ONLY TWO TRANSACTIONS ARE ALLOWED

Recursion

Code

int f(vector<int>& prices, int n, int ind, int buy, int cap)
{
    if (ind == n || cap == 0)
        return 0;

    if (buy == 1) {
        int buyStock = -prices[ind] + f(prices, n, ind + 1, 0, cap);
        int dontBuyStock = f(prices, n, ind + 1, 1, cap);
        return max(buyStock, dontBuyStock);
    } else {
        int sellStock = prices[ind] + f(prices, n, ind + 1, 1, cap - 1);
        int dontSellStock = f(prices, n, ind + 1, 0, cap);
        return max(sellStock, dontSellStock);
    }
}

int maxProfit(vector<int>& prices, int n)
{
    return f(prices, n, 0, 1, 2);
}

Memoization(top-down)

Code

int f(vector<int>& prices, int n, int ind, int buy, int cap, vector<vector<vector<int>>>& dp)
{
    if (ind == n || cap == 0)
        return 0;

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

    if (buy == 1) {
        int buyStock = -prices[ind] + f(prices, n, ind + 1, 0, cap, dp);
        int dontBuyStock = f(prices, n, ind + 1, 1, cap, dp);
        return dp[ind][buy][cap] = max(buyStock, dontBuyStock);
    } else {
        int sellStock = prices[ind] + f(prices, n, ind + 1, 1, cap - 1, dp);
        int dontSellStock = f(prices, n, ind + 1, 0, cap, dp);
        return dp[ind][buy][cap] = max(sellStock, dontSellStock);
    }
}

int maxProfit(vector<int>& prices, int n)
{
    vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(2, vector<int>(3, -1)));
    return f(prices, n, 0, 1, 2, dp);
}

Tabulation(bottom-up)

Code

int maxProfit(vector<int>& prices, int n)
{
    vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(2, vector<int>(3, 0)));

    for (int ind = n - 1; ind >= 0; ind--) {
        for (int buy = 0; buy <= 1; buy++) {
            for (int cap = 1; cap <= 2; cap++) {
                if (buy == 1) {
                    int buyStock = -prices[ind] + dp[ind + 1][0][cap];
                    int dontBuyStock = dp[ind + 1][1][cap];
                    dp[ind][buy][cap] = max(buyStock, dontBuyStock);
                } else {
                    int sellStock = prices[ind] + dp[ind + 1][1][cap - 1];
                    int dontSellStock = dp[ind + 1][0][cap];
                    dp[ind][buy][cap] = max(sellStock, dontSellStock);
                }
            }
        }
    }
    return dp[0][1][2];
}

Space Optimized Tabulation(bottom-up)

int maxProfit(vector<int>& prices, int n)
{
    vector<vector<int>> curr(2, vector<int>(3, 0)), after(2, vector<int>(3, 0));

    for (int ind = n - 1; ind >= 0; ind--) {
        for (int buy = 0; buy <= 1; buy++) {
            for (int cap = 1; cap <= 2; cap++) {
                if (buy == 1) {
                    int buyStock = -prices[ind] +after[0][cap];
                    int dontBuyStock =after[1][cap];
                    curr[buy][cap] = max(buyStock, dontBuyStock);
                } else {
                    int sellStock = prices[ind] +after[1][cap - 1];
                    int dontSellStock =after[0][cap];
                    curr[buy][cap] = max(sellStock, dontSellStock);
                }
            }
        }
        after = curr; // Don't forget this line
    }
    return after[1][2];
}

Other Solution