|
| 1 | +Say you have an array for which the ith element is the price of a given stock on day i. |
| 2 | + |
| 3 | +Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: |
| 4 | + |
| 5 | +You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). |
| 6 | +After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) |
| 7 | +Example: |
| 8 | + |
| 9 | +Input: [1,2,3,0,2] |
| 10 | +Output: 3 |
| 11 | +Explanation: transactions = [buy, sell, cooldown, buy, sell] |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +// Recursive (TLE) |
| 22 | + |
| 23 | +class Solution { |
| 24 | +public: |
| 25 | + |
| 26 | + int maxProfitUtil(int i, bool buyOrSell, vector<int>& prices){ |
| 27 | + if(i>=prices.size()) |
| 28 | + return 0; |
| 29 | + int profit=0; |
| 30 | + if(buyOrSell==false){ |
| 31 | + int buy=maxProfitUtil(i+1, true, prices)-prices[i]; // true---> to sell next |
| 32 | + int noBuy=maxProfitUtil(i+1, false, prices); // false----> to buy next |
| 33 | + profit=max(buy, noBuy); |
| 34 | + } else { |
| 35 | + int sell=maxProfitUtil(i+2, false, prices)+prices[i]; // false--> to buy next i+2 ---> for cooldown |
| 36 | + int noSell=maxProfitUtil(i+1, true, prices); // true---->to sell next |
| 37 | + profit=max(sell, noSell); |
| 38 | + } |
| 39 | + return profit; |
| 40 | + } |
| 41 | + |
| 42 | + int maxProfit(vector<int>& prices) { |
| 43 | + // buyOrSell false----> buy true----> sell |
| 44 | + return maxProfitUtil(0, false, prices); // index, buyOrSell, prices |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +// DP |
| 55 | + |
| 56 | +class Solution { |
| 57 | +public: |
| 58 | + |
| 59 | + unordered_map<string, int> dp; |
| 60 | + |
| 61 | + int maxProfitUtil(int i, bool buyOrSell, vector<int>& prices){ |
| 62 | + if(i>=prices.size()) |
| 63 | + return 0; |
| 64 | + string id=to_string(i)+to_string(buyOrSell); |
| 65 | + if(dp.find(id)!=dp.end()) return dp[id]; |
| 66 | + |
| 67 | + int profit=0; |
| 68 | + if(buyOrSell==false){ |
| 69 | + int buy=maxProfitUtil(i+1, true, prices)-prices[i]; // true---> to sell next |
| 70 | + int noBuy=maxProfitUtil(i+1, false, prices); // false----> to buy next |
| 71 | + profit=max(buy, noBuy); |
| 72 | + } else { |
| 73 | + int sell=maxProfitUtil(i+2, false, prices)+prices[i]; // false--> to buy next i+2 ---> for cooldown |
| 74 | + int noSell=maxProfitUtil(i+1, true, prices); // true---->to sell next |
| 75 | + profit=max(sell, noSell); |
| 76 | + } |
| 77 | + dp[id]=profit; |
| 78 | + return profit; |
| 79 | + } |
| 80 | + |
| 81 | + int maxProfit(vector<int>& prices) { |
| 82 | + // buyOrSell false----> buy true----> sell |
| 83 | + return maxProfitUtil(0, false, prices); // index, buyOrSell, prices |
| 84 | + } |
| 85 | +}; |
0 commit comments