Given a list containing future price predictions of two different stocks for the next n-days, find the maximum profit that can be 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, you can either sell a single share of the first stock or a single share of the second stock or sell nothing.
For example,
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

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

Maximum profit earned is 25

Java

Download   Run Code

Output:

Maximum profit earned is 25

Python

Download   Run Code

Output:

Maximum profit earned is 25

Download   Run Code
Output:

Maximum profit earned is 25

Download   Run Code
Output:

Maximum profit earned is 25

Download   Run Code
Output:

Maximum profit earned is 25

The above solution’s time complexity 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 sub-problem T(i) is computed. If we are ever asked to compute it again, return the cached result and not recompute it. The following 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:

C

Download   Run Code

Output:

Maximum profit earned is 25

Java

Download   Run Code

Output:

Maximum profit earned is 25

Python

Download   Run Code

Output:

Maximum profit earned is 25

Download   Run Code
Output:

Maximum profit earned is 25

Download   Run Code
Output:

Maximum profit earned is 25

Download   Run Code
Output:

Maximum profit earned is 25

The time complexity of the above solution is O(n). The auxiliary space used 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). The need for an auxiliary array can be eliminated by keeping track of only the last two subproblems’ solutions. Following is a better implementation in C, Java, and Python without using an auxiliary array.

C

Download   Run Code

Output:

Maximum profit earned is 25

Java

Download   Run Code

Output:

Maximum profit earned is 25

Python

Download   Run Code

Output:

Maximum profit earned is 25

Download   Run Code
Output:

Maximum profit earned is 25

Download   Run Code
Output:

Maximum profit earned is 25

Download   Run Code
Output:

Maximum profit earned is 25

The time complexity of the above solution is O(n) and auxiliary space used by the program is O(1).

source