|
| 1 | +/* |
| 2 | +188. Best Time to Buy and Sell Stock IV |
| 3 | +Hard |
| 4 | +
|
| 5 | +You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k. |
| 6 | +Find the maximum profit you can achieve. You may complete at most k transactions: i.e. you may buy at most k times and sell at most k times. |
| 7 | +Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again). |
| 8 | +
|
| 9 | +Example 1: |
| 10 | +Input: k = 2, prices = [2,4,1] |
| 11 | +Output: 2 |
| 12 | +Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2. |
| 13 | +
|
| 14 | +Example 2: |
| 15 | +Input: k = 2, prices = [3,2,6,5,0,3] |
| 16 | +Output: 7 |
| 17 | +Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. |
| 18 | +
|
| 19 | +Constraints: |
| 20 | +
|
| 21 | +1 <= k <= 100 |
| 22 | +1 <= prices.length <= 1000 |
| 23 | +0 <= prices[i] <= 1000 |
| 24 | +*/ |
| 25 | +class Solution { |
| 26 | + //static int[][][] memo = new int[1001][2][101]; |
| 27 | + static HashMap<String,Integer> map = new HashMap<>(); |
| 28 | + public int maxProfit(int k, int[] prices) { |
| 29 | + //1. Recursion:: |
| 30 | + //return solveRec(prices,0,0,k); |
| 31 | + |
| 32 | + // 2. Memoization:: |
| 33 | + // 3 chnaging parameter:) Use either 3dp or hashmap:) |
| 34 | + //return solveMemo(prices,0,0,k); |
| 35 | + |
| 36 | + //3d -dp way:) |
| 37 | + // for(int[][] arr:memo){ |
| 38 | + // for(int[] si:arr){ |
| 39 | + // Arrays.fill(si,-1); |
| 40 | + // } |
| 41 | + // } |
| 42 | + // return solveMemo(prices,0,0, k); |
| 43 | + |
| 44 | + return solveTab(prices,k); |
| 45 | + |
| 46 | + //return solveTabSpaceOptmised(prices); |
| 47 | + } |
| 48 | + public static int solveRec(int[] prices,int indx,int buy,int cnt){ |
| 49 | + if(indx==prices.length) return 0; |
| 50 | + if(cnt<0) return 0; |
| 51 | + |
| 52 | + int profit=0; |
| 53 | + if(buy==0){ |
| 54 | + // buy:: |
| 55 | + profit =-prices[indx]+solveRec(prices,indx+1,1,cnt-1); |
| 56 | + }else{ |
| 57 | + //sell:: |
| 58 | + profit =prices[indx]+solveRec(prices,indx+1,0,cnt); |
| 59 | + } |
| 60 | + int skip =solveRec(prices,indx+1,buy,cnt); |
| 61 | + |
| 62 | + return Math.max(skip,profit); |
| 63 | + } |
| 64 | + public static int solveMemo(int[] prices,int indx,int buy,int cnt){ |
| 65 | + if(indx==prices.length) return 0; |
| 66 | + if(cnt<0) return 0; |
| 67 | + |
| 68 | + String str =indx+"-"+buy+"-"+cnt; |
| 69 | + if(map.containsKey(str)){ |
| 70 | + return map.get(str); |
| 71 | + } |
| 72 | + int profit=0; |
| 73 | + if(buy==0){ |
| 74 | + // buy:: |
| 75 | + profit =-prices[indx]+solveMemo(prices,indx+1,1,cnt-1); |
| 76 | + }else{ |
| 77 | + //sell:: |
| 78 | + profit =prices[indx]+solveMemo(prices,indx+1,0,cnt); |
| 79 | + } |
| 80 | + int skip =solveMemo(prices,indx+1,buy,cnt); |
| 81 | + map.put(str,Math.max(skip,profit)); |
| 82 | + return Math.max(skip,profit); |
| 83 | + } |
| 84 | + public static int solveMemo(int[] prices,int indx,int buy,int k){ |
| 85 | + if(k<0) return 0; |
| 86 | + if(indx>=prices.length) return 0; |
| 87 | + |
| 88 | + if(memo[indx][buy][k]!=-1){ |
| 89 | + return memo[indx][buy][k]; |
| 90 | + } |
| 91 | + int profit =0; |
| 92 | + if(buy==0){ |
| 93 | + profit = Math.max(-prices[indx]+solveMemo(prices,indx+1,1,k),solveMemo(prices,indx+1,buy,k)); |
| 94 | + }else{ |
| 95 | + profit = Math.max(prices[indx]+solveMemo(prices,indx+1,0,k-1),solveMemo(prices,indx+1,buy,k)); |
| 96 | + } |
| 97 | + return memo[indx][buy][k]=profit; |
| 98 | + } |
| 99 | + public static int solveTab(int[] prices,int k){ |
| 100 | + //Tabulation::) |
| 101 | + int n=prices.length; |
| 102 | + int tab[][][] = new int[n+1][2][k+1]; |
| 103 | + |
| 104 | + for(int ind=n-1;ind>=0;ind--){ |
| 105 | + for(int buy=0;buy<=1;buy++){ |
| 106 | + for(int cap=1;cap<=k;cap++){ |
| 107 | + if (buy == 0) { |
| 108 | + tab[ind][buy][cap] = Math.max(0 + tab[ind + 1][0][cap], |
| 109 | + -prices[ind] + tab[ind + 1][1][cap]); |
| 110 | + } |
| 111 | + if (buy == 1) { |
| 112 | + tab[ind][buy][cap] = Math.max(0 + tab[ind + 1][1][cap], |
| 113 | + prices[ind] + tab[ind + 1][0][cap - 1]); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + return tab[0][0][k]; |
| 119 | + } |
| 120 | + public static int solveTabSpaceOptmised(int[] prices){ |
| 121 | + //Sapce Optimied:: Tabulation::) |
| 122 | + int n=prices.length; |
| 123 | + |
| 124 | + int ahead[][] = new int[2][3]; |
| 125 | + int curr[][] = new int[2][3]; |
| 126 | + |
| 127 | + for(int ind=n-1;ind>=0;ind--){ |
| 128 | + for(int buy=0;buy<=1;buy++){ |
| 129 | + for(int cap=1;cap<=2;cap++){ |
| 130 | + if (buy == 0) { |
| 131 | + curr[buy][cap] = Math.max(0 + ahead[0][cap], |
| 132 | + -prices[ind] + ahead[1][cap]); |
| 133 | + } |
| 134 | + if (buy == 1) { |
| 135 | + curr[buy][cap] = Math.max(0 + ahead[1][cap], |
| 136 | + prices[ind] + ahead[0][cap - 1]); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + for (int i = 0; i < 2; i++) { |
| 141 | + for (int j = 1; j < 3; j++) { |
| 142 | + ahead[i][j] = curr[i][j]; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return ahead[0][2]; |
| 148 | + } |
| 149 | +} |
0 commit comments