05. Stock buy and sell II
The problem can be found at the following link: Question Link
My Approach
Our first intuition for this problem is to use a greedy approach. However, we soon realize that there are many cases where the greedy approach fails. Therefore, we need to consider all possibilities to solve this problem using DP.
To begin solving this, I have to derive a recurrence relation for this problem.
Note: In the following explanation, i
represents the current index, and canBuy
indicates the current situation where you can either buy
or have already bought shares (which means you need to sell
it).
Let's assume that at any index of the array, you have the choice to either buy or sell shares. We need to consider the possibilities:
First, we don't involve the share trading at the current index and move forward with the same buying/selling condition.
If you can buy shares, then you buy the share (decreasing the sum by the price of the share) and set
canBuy
to0
, indicating that you now want to sell the shares.If you can sell shares (means
canBuy = 0
), then you add the sum to the price of the share (indicating that you sell the share and store the profit/loss in the sum), and now setcanBuy
to1
, meaning that you are now able to buy shares again.
At each recursive step, you need to return the maximum value obtained from either the first condition (don't involve in trading), the second condition (buy share), or the combination of the first condition(don't involve in trading) and third condition (sell shares).
By considering these possibilities, we can effectively solve the problem.
Here is the code with the recurrence relation, but this time it result in a Time Limit Exceeded (TLE) due to large number of recursion involvement.
Recurrence recursive code (c++) (Gives TLE)
Optimized approach
To optimize this recursive code, we can use the Tabulation method of Dynamic Programming (DP) instead of the top-down recursive approach. By utilizing the bottom-up Tabulation approach, we can improve the efficiency of the solution. It's important to note that the recurrence relation remains the same for both approaches.
Time and Auxiliary Space Complexity
Time Complexity :
O(n)
since we are traversing only n * 2 timesAuxiliary Space Complexity :
O(n*2)
auxiliary space complexity for 2-d DP array
Code (C++) (Tabulation)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated