Important Links : 'Buy & Sell Stock - Infinite Transactions Allowed'.
Dear reader, welcome to the article on the problem named. It is the 2nd problem in the series Buy & Sell Stock.
Buy & Sell Stock - Infinite Transactions Allowed
"Experience is the name everyone gives to their mistakes."
~ Oscar Wilde
Important Links : 'Buy & Sell Stock - Infinite Transactions Allowed'.
Dear reader, welcome to the article on the problem named. It is the 2nd problem in the series Buy & Sell Stock.
Note - There can be no overlapping transactions. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy). We cannot first buy 2 stocks and then sell them, i.e. states like 'BBSS' are not allowed.
Nobody will laugh at you, if you didn't know this! It is very trivial, but still, I want to let you know that you cannot sell a stock before buying it! You cannot have a state "SB".
Consider the array of days as : [7,1,5,3,6,4] . The maximum profit can be achieved by (days are 0 based indexed):
- Buying at day 1 at price 1
- Selling at day 2 at price 5
- Buying at day 3 at price 3
- Selling at day 4 at price 6
The total profit is ((5 - 1) + (6 - 3)) = 7.
Since, we can make any number of transactions, hence we need to find all the buying - selling point pairs, such that their cumulative profit is maximum possible.
Let us try to plot the array on a price vs day graph, and analyze how we can extract maximum profit:
The only points of interest are the peaks and the valleys.
Hence, we can pair all the corresponding valleys with peaks and the difference between their values will be the profit. We will add all such pairs differences to our result.
Q) Why cannot we take dips (peak to valley points) into consideration?
R) If you will not take peak to valley point, then we are buying for a higher price and selling for a lower price, hence making a loss. We cannot bear such losses, when our career is concerned.
Also, we cannot make a pair of any point in the dip with the peak, because the profit of peak minus this dip point will be less than the profit of peak minus valley point.
The key point is we need to consider every peak immediately following a valley to maximize the profit. In case we skip one of the peaks (trying to obtain more profit), we will end up losing the profit over one of the transactions leading to an overall lesser profit.
How can we make pairs of peaks and valleys?
Note: Before reading the Code, we recommend that you must try to come up with the solution on your own. Now, hoping that you have tried by yourself, here is the Java code.
java; true";
Java Code is written and explained by our team in the solution video from timestamp.
Since, we are traversing through the array of n integers (representing days), and doing constant work inside the loop, hence time complexity will be O(N).
We have used few integer variables, hence constant O(1) auxiliary space is required.
Important Links : 'Buy And Sell Stock - With Transaction Fee - Infinite Transactions'.
Subscribe to Pepcoding's youtube channel for more such amazing content on Data Structures & Algorithms and follow the resources available for all students in the resources section of Pepcoding's website.
You can suggest any improvements to the article on our telegram channel, or on the youtube channel's comment section.