Given a list containing future prediction of share prices, find the maximum profit earned by buying and selling shares any number of times with the constraint, a new transaction can only start after the previous transaction is complete, i.e., we can only hold at most one share at a time.

For example,

Stock Prices: {1, 5, 2, 3, 7, 6, 4, 5}
 
Total profit earned is 10
 
Buy on day 1 and sell on day 2
Buy on day 3 and sell on day 5
Buy on day 7 and sell on day 8
 
 
Stock Prices: {10, 8, 6, 5, 4, 2}
 
Total profit earned is 0

Practice this problem

There are several variations to the above problem:

  1. If we are allowed to stock only once, we can find the maximum difference between two elements in the array, where the smaller element appears before the larger element.
  2. If we are allowed to stock shares at most twice, we can follow the approach discussed here.
  3. If we are allowed to stock shares at most k times, we can follow the approach discussed here.

 
The current problem allows us to make unlimited stock transactions. The idea is to traverse the given list of prices and find a local minimum of every increasing sequence. For example, the increasing sequences of length 2 or more in the array {1, 5, 2, 3, 7, 6, 4, 5} are {1, 5}, {2, 3, 7}, and {4, 5}. The local minimum of each sequence is 1, 2 and 4, respectively. We can gain maximum profit if we buy the shares at the starting of every increasing sequence (local minimum) and sell them at the end of the increasing sequence (local maximum).

Following is the C, Java, and Python program that demonstrates it:

C


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Output:
 
Buy on day 1 and sell on day 2
Buy on day 3 and sell on day 5
Buy on day 7 and sell on day 8
 
Total profit earned is 10

The time complexity of the above solution is O(n) and doesn’t require any extra space, where n is the total number of given days.