Best Time to Buy and Sell Stock III

Hard
0/120
profile
Contributed by
158 upvotes
Asked in companies
MyntraGrowwAtlassian

Problem statement

You are Harshad Mehta’s friend. He told you the price of a particular stock for the next ‘n’ days.


You are given an array ‘prices’ which such that ‘prices[i]’ denotes the price of the stock on the ith day.


You don't want to do more than 2 transactions. Find the maximum profit that you can earn from these transactions.


Note

1. Buying a stock and then selling it is called one transaction.

2. You are not allowed to do multiple transactions at the same time. This means you have to sell the stock before buying it again. 
Example:
Input: ‘n’ = 7, ‘prices’ = [3, 3, 5, 0, 3, 1, 4].

Output: 6

Explanation: 
The maximum profit can be earned by:
Transaction 1: Buying the stock on day 4 (price 0) and then selling it on day 5 (price 3). 
Transaction 2: Buying the stock on day 6 (price 1) and then selling it on day 6 (price 4).
Total profit earned will be (3 - 0) + ( 4 - 1) = 6. 
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of each test case contains an integer 'n' denoting the number of days. 

The second line of each test case contains 'n' space-separated integers, the array ‘prices’.
Output Format :
For each test case, return an integer denoting the maximum profit. 
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Sample Input 1:
6
1 3 1 2 4 8
Sample Output 1:
9
Explanation Of Sample Input 1 :
The maximum profit can be earned by:
Transaction 1: Buying the stock on day 1 (price 1) and then selling it on day 2 (price 3). 
Transaction 2: Buying the stock on day 3 (price 1) and then selling it on day 6 (price 8).
Total profit earned will be (3 - 1) + ( 8 - 1) = 9. 
Sample Input 2:
5
5 4 3 2 1
Sample Output 2:
0
Explanation of Sample Output 2:
It's better not to do any transactions as the stock prices are decreasing. 
Expected time complexity:
The expected time complexity is O(n).
Constraints:
1 <= ‘n’ <= 10^6
0 <= ‘prices[i]’ <= 10^3

Where ‘prices[i]’ is the stock price on ith day. 

Time Limit: 1 sec.
Hint

Can you do this recursively? Try to solve the problem by solving its subproblems first.

Approaches (4)
Recursion

This problem can be solved by solving its subproblems and then combining the solutions of the solved subproblems to solve the original problem. We will do this using recursion.

Basically, we have to buy the stock at the minimum possible price and sell at the maximum possible price, keeping in mind that we have to sell the stock before buying it again.

 

 

Below is the detailed algorithm: 

 

  1. Call a recursive function ‘maxProfitHelper’ by passing four parameters i.e., 'prices’, starting index, number of transactions, and a boolean variable ‘IsBought’. ‘IsBought’ will help us engage in just one transaction at a time. Initially, ‘IsBought’ is false.

 

 

maxProfitHelper(‘prices’, ‘index’, ‘trans’, ‘isBought’): 

 

  1. Base Condition: When we are out of days, or the transactions left are 0, return 0 as you can not do anything.
  2. Now we have three options:
    • If ‘isBought’ is false:
      • Buy the stock: Recursively find the profit made by the remaining items by setting ‘isBought’ as true and ‘index’ as ‘index’ + 1. Note that buying a stock is a loss of ‘prices[index]’.
    • If ‘isBought’ is true:
      • Sell the stock: Recursively find the profit made by the remaining items by decreasing the transactions by 1 and ‘index’ as ‘index’ + 1. Note that selling a stock is a profit of ‘prices[index]’.
    • Skip the day, Recursively find the profit made by the remaining items by setting ‘index’ as ‘index’ + 1.
  3. Return the maximum profit obtained by any of the above options.
Time Complexity

O(2 ^ n), where n is the number of days. 

 

We have three options each day; either skip the day, buy on that day or sell on that day. But we can only engage in one transaction at a time, each recursive call ends up in two recursive calls.

 

Hence the final time complexity will be O(2 ^ n).

Space Complexity

O(n), where n is the number of days. 

 

We are not using any external data structure. Only recursion stack space of O(n) size is used by the algorithm.

 

Hence the final space complexity will be O(n).

Code Solution
(100% EXP penalty)
Best Time to Buy and Sell Stock III
Full screen
Console