Given a list containing future price predictions of two different stocks for the next n–days, find the maximum profit earned by selling the stocks with a constraint that the second stock can be sold, only if no transaction happened on the previous day for any of the stock.

Assume that the given prices are relative to the buying price. Each day, we can either sell a single share of the first stock or a single share of the second stock or sell nothing.

For example,

Input:
 
Day    Price(x)  Price(y)
1        5        8
2        3        4
3        4        3
4        6        5
5        3        10
 
Output: Maximum profit earned is 25
 
Explanation:
 
Day 1: Sell stock y at a price of 8
Day 2: Sell stock x at a price of 3
Day 3: Sell stock x at a price of 4
Day 4: Don’t sell anything
Day 5: Sell stock y at a price of 10

Practice this problem

The idea is to use recursion to solve this problem in a top-down manner. To find the maximum profit till n'th day, there are two possible options:

  1. Sell the first stock on the n'th day, and then recursively calculate the maximum profit till the (n-1)'th day.
  2. Sell the second stock on the n'th day, skip the (n-1)'th day, and then recursively calculate the maximum profit till the (n-2)'th day.

Accordingly, consider the maximum profit possible by either selling the first stock or the second stock on the n'th day. The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

The maximum profit earned is 25

Java


Download  Run Code

Output:

The maximum profit earned is 25

Python


Download  Run Code

Output:

The maximum profit earned is 25

The time complexity of the above solution is exponential since the code is doing a lot of redundant work. For example, for solving the problem T(n), a recursive call is made to find the maximum profit for both T(n-1) and T(n-2). However, finding the maximum profit for T(n-1) also requires finding the maximum profit for T(n-2). As the recursion grows deeper, more and more of this type of unnecessary repetition occurs.

 
This repetition can be avoided with dynamic programming. The idea is to cache each time any subproblem T(i) is computed. If we are ever asked to compute it again, return the cached result and not recompute it. The bottom-up approach computes the maximum earnings of all days, using the already computed profit of past days. The maximum earnings T[i] possible till the i'th day can be calculated by taking the maximum of the below actions:

  1. Sell the first stock on the i'th day, and add the profit to the maximum earnings till the (i-1)'th day.
  2. Sell the second stock on the i'th day, and add the profit to the maximum earnings till the (i-2)'th day.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

The maximum profit earned is 25

Java


Download  Run Code

Output:

The maximum profit earned is 25

Python


Download  Run Code

Output:

The maximum profit earned is 25

The time complexity of the above solution is O(n), where n is the total number of given days. The auxiliary space required by the program is O(n) for the auxiliary array and requires no recursion.

 
Note that the value of T(n) is calculated in constant time using the values of T(n-1) and T(n-2). We can eliminate the need for an auxiliary array by keeping track of only the last two subproblems’ solutions. Following is an efficient implementation in C++, Java, and Python without using an auxiliary array:

C++


Download  Run Code

Output:

The maximum profit earned is 25

Java


Download  Run Code

Output:

The maximum profit earned is 25

Python


Download  Run Code

Output:

The maximum profit earned is 25

The time complexity of the above solution is O(n) and requires O(n) extra space, where n is the total number of given days.