|
| 1 | +/* |
| 2 | +309. Best Time to Buy and Sell Stock with Cooldown |
| 3 | +Medium |
| 4 | +
|
| 5 | +You are given an array prices where prices[i] is the price of a given stock on the ith day. |
| 6 | +Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions: |
| 7 | +After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day). |
| 8 | +Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again). |
| 9 | +
|
| 10 | +Example 1: |
| 11 | +Input: prices = [1,2,3,0,2] |
| 12 | +Output: 3 |
| 13 | +Explanation: transactions = [buy, sell, cooldown, buy, sell] |
| 14 | +
|
| 15 | +Example 2: |
| 16 | +Input: prices = [1] |
| 17 | +Output: 0 |
| 18 | + |
| 19 | +Constraints: |
| 20 | +1 <= prices.length <= 5000 |
| 21 | +0 <= prices[i] <= 1000 |
| 22 | +*/ |
| 23 | +static int memo[][] = new int[5001][2]; |
| 24 | + public int maxProfit(int[] prices) { |
| 25 | + //Recursion:) |
| 26 | + // TC : O(2^n) SC: O(1)+O(n) --aux space:: |
| 27 | + //return solveRec(prices,0,0); |
| 28 | + |
| 29 | + //Memoisation:) |
| 30 | + // TC : O(n*2) SC: O(1)+O(n)--aux space:: |
| 31 | + // Arrays.stream(memo).forEach(a->Arrays.fill(a,-1)); |
| 32 | + // return solveMemo(prices,0,0); |
| 33 | + |
| 34 | + // TC : O(n*2) SC : O(n*2) |
| 35 | + //Bottom Up approch :: |
| 36 | + //return solveTab(prices); |
| 37 | + |
| 38 | + // TC : O(n) SC: O(n) |
| 39 | + //return solveTabOpt(prices); |
| 40 | + |
| 41 | + // TC : O(n) SC:O(1) |
| 42 | + return solveTabSpaceOpt(prices); |
| 43 | + } |
| 44 | + private int solveRec(int[] prices ,int indx,int buy){ |
| 45 | + //Base Case :: |
| 46 | + if(indx>=prices.length) return 0; |
| 47 | + int profit=0; |
| 48 | + if(buy==0 ){ |
| 49 | + profit =Math.max(-prices[indx]+solveRec(prices,indx+1,1),solveRec(prices,indx+1,0)); |
| 50 | + }else{ |
| 51 | + profit =Math.max(prices[indx]+solveRec(prices,indx+2,0),solveRec(prices,indx+1,1)); |
| 52 | + } |
| 53 | + return profit; |
| 54 | + } |
| 55 | + private int solveMemo(int[] prices ,int indx,int buy){ |
| 56 | + //Base Case :: |
| 57 | + if(indx>=prices.length) return 0; |
| 58 | + |
| 59 | + if(memo[indx][buy]!=-1){ |
| 60 | + return memo[indx][buy]; |
| 61 | + } |
| 62 | + int profit=0; |
| 63 | + if(buy==0 ){ |
| 64 | + profit =Math.max(-prices[indx]+solveMemo(prices,indx+1,1),solveMemo(prices,indx+1,0)); |
| 65 | + }else{ |
| 66 | + profit =Math.max(prices[indx]+solveMemo(prices,indx+2,0),solveMemo(prices,indx+1,1)); |
| 67 | + } |
| 68 | + return memo[indx][buy]=profit; |
| 69 | + } |
| 70 | + private int solveTab(int[] prices){ |
| 71 | + //Tabulation::) |
| 72 | + int n =prices.length; |
| 73 | + int tab[][] = new int[n+2][2]; |
| 74 | + |
| 75 | + for(int indx=n-1;indx>=0;indx--){ |
| 76 | + for(int j=0;j<=1;j++){ |
| 77 | + if(j==0 ){ |
| 78 | + tab[indx][j]=Math.max(-prices[indx]+tab[indx+1][1],tab[indx+1][0]); |
| 79 | + } |
| 80 | + if(j==1){ |
| 81 | + tab[indx][j]=Math.max(prices[indx]+tab[indx+2][0],tab[indx+1][1]); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + return tab[0][0]; |
| 86 | + } |
| 87 | + private int solveTabOpt(int[] prices){ |
| 88 | + //Tabulation::) |
| 89 | + int n =prices.length; |
| 90 | + int tab[][] = new int[n+2][2]; |
| 91 | + |
| 92 | + for(int indx=n-1;indx>=0;indx--){ |
| 93 | + tab[indx][0]=Math.max(-prices[indx]+tab[indx+1][1],tab[indx+1][0]); |
| 94 | + tab[indx][1]=Math.max(prices[indx]+tab[indx+2][0],tab[indx+1][1]); |
| 95 | + } |
| 96 | + return tab[0][0]; |
| 97 | + } |
| 98 | + private int solveTabSpaceOpt(int[] prices){ |
| 99 | + //Tabulation::) Striver APPROCH:: |
| 100 | + int n =prices.length; |
| 101 | + int ahead2[]=new int[2]; |
| 102 | + int ahead[] =new int[2]; |
| 103 | + int curr[] =new int[2]; |
| 104 | + |
| 105 | + for(int indx=n-1;indx>=0;indx--){ |
| 106 | + curr[0]=Math.max(-prices[indx]+ahead[1],ahead[0]); |
| 107 | + curr[1]=Math.max(prices[indx]+ahead2[0],ahead[1]); |
| 108 | + |
| 109 | + //Now updated :: ahead2 to ahead2,ahead1 to curr |
| 110 | + System.arraycopy(ahead,0,ahead2,0,2); |
| 111 | + System.arraycopy(curr,0,ahead,0,2); |
| 112 | + } |
| 113 | + return curr[0]; |
| 114 | + } |
0 commit comments