Posts Best Time to Buy and Sell Stock III
Post
Cancel

Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

**Note: **You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

1
2
3
4
Input: [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
             Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.

Example 2:

1
2
3
4
5
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
             Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
             engaging multiple transactions at the same time. You must sell before buying again.

Example 3:

1
2
3
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

Idea

The main thing we are trying to formulate is that on our Kth tansaction we want to maximize our profit.

To put it in terms of math term suppose on Kth transaction we want to sell our stock at price i.

the profit we make is price[i] - price[j] suppose we bought it on price[j]. Now the possible values for j is 0…i-1.

But we also need to add the profit we made on K-1 th transaction. So the final DP equation becomes

DP[ K, i ] = MAX ( DP[K,i-1], price[i] - price[j] + DP[K-1,j]) where j = 0 … i-1

To better understand this imagine your self in sitution where you want to invest in stock market. Inatially you don’t have any money so you borrow it from someone else. Till now you balance is negative. Now you invest that money in stock market and you make some profit which is equal to sell_price - balance.

Now you again invest some money from stock market but this time you have profit from previous sell hence you balance is last_profit - buy2. Now again you want to sell this at a price which makes profit hence the net profit would be

sell2-buy2+last_profit = sell2-buy2+( sell1-buy1 )

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    int maxProfit(vector<int>& price) {
        int n=price.size();
        int buy1=INT_MAX,buy2=INT_MAX; // Min this
        int sell1=0,sell2=0; //Max this
        // this means we start will  0 amt
        
        for(int i=0;i<n;i++)
        {
            buy1 = min(buy1,price[i]);
            sell1 = max(sell1,price[i]-buy1);
            //sell1 is profit I make
            
            buy2 = min(buy2, price[i]-sell1); // min the buyin which equal to price[i] - sell1(profit/loss)
            sell2 = max(sell2,price[i]-buy2);
            
        }
        return max(sell2,0);
    }
};

This post is licensed under CC BY 4.0 by the author.