75-days-dsa-challenge

Ninja technique🥷 to ACE DSA Interviews.

View the Project on GitHub

123. Best Time to Buy and Sell Stock III (Hard)

Recursive Solution (TLE)

Code

class Solution {
private:
    int helper(vector<int>& prices, int n, int day, int transRem)
    {
        // base cases
        if (day == n || transRem == 0)
            return 0;

        // do nothing
        int doNothing = helper(prices, n, day + 1, transRem);
        int doSomething = 0;

        // do something (buy/sell)
        bool buy = (transRem % 2 == 0);
        if (buy) { // buy
            doSomething = -prices[day] + helper(prices, n, day + 1, transRem - 1);
        } else { // sell
            doSomething = prices[day] + helper(prices, n, day + 1, transRem - 1);
        }

        return max(doNothing, doSomething);
    }

public:
    int maxProfit(vector<int>& prices)
    {
        int n = prices.size();
        return helper(prices, n, 0, 4);
    }
};

Memoization (AC)

Code

class Solution {
private:
    int helper(vector<int>& prices, int n, int day, int transRem, vector<vector<int>>& dp)
    {
        // base cases
        if (day == n || transRem == 0)
            return 0;

        // if we have already calculated the result, return it
        if (dp[day][transRem] != -1)
            return dp[day][transRem];

        // do nothing
        int doNothing = helper(prices, n, day + 1, transRem, dp);
        int doSomething = 0;

        // do something (buy/sell)
        bool buy = (transRem % 2 == 0);
        if (buy) { // buy
            doSomething = -prices[day] + helper(prices, n, day + 1, transRem - 1, dp);
        } else { // sell
            doSomething = prices[day] + helper(prices, n, day + 1, transRem - 1, dp);
        }

        return dp[day][transRem] = max(doNothing, doSomething);
    }

public:
    int maxProfit(vector<int>& prices)
    {
        int n = prices.size();
        vector<vector<int>> dp(n, vector<int>(5, -1));
        return helper(prices, n, 0, 4, dp);
    }
};

Tabulation (AC)

Code

class Solution {
public:
    int maxProfit(vector<int>& prices)
    {
        int n = prices.size();
        vector<vector<int>> dp(n+1, vector<int>(5, 0));
        for (int i = n; i >= 0; i--) {
            for (int j = 0; j < 5; j++) {
                if (i == n || j == 0) {
                    dp[i][j] = 0;
                } else {
                    int doNothing = dp[i + 1][j];
                    int doSomething = 0;
                    bool buy = (j % 2 == 0);
                    if (buy) { // buy
                        doSomething = -prices[i] + dp[i + 1][j - 1];
                    } else { // sell
                        doSomething = prices[i] + dp[i + 1][j - 1];
                    }
                    dp[i][j] = max(doNothing, doSomething);
                }
            }
        }
        return dp[0][4];
    }
};

Tabulation | space optimization (AC)

Code

class Solution {
public:
    int maxProfit(vector<int>& prices)
    {
        int n = prices.size();
        vector<vector<int>> dp(2, vector<int>(5, 0));
        for (int i = n; i >= 0; i--) {
            for (int j = 0; j < 5; j++) {
                if (i == n || j == 0) {
                    dp[i % 2][j] = 0;
                } else {
                    int doNothing = dp[(i + 1) % 2][j];
                    int doSomething = 0;
                    bool buy = (j % 2 == 0);
                    if (buy) { // buy
                        doSomething = -prices[i] + dp[(i + 1) % 2][j - 1];
                    } else { // sell
                        doSomething = prices[i] + dp[(i + 1) % 2][j - 1];
                    }
                    dp[i % 2][j] = max(doNothing, doSomething);
                }
            }
        }
        return dp[0][4];
    }
};