|
| 1 | +// 38. Calculate Maximum Profit (Buying/Selling Share Twice) |
| 2 | + |
| 3 | +/* |
| 4 | +In this approach, we are going to store the maximum possible profit of every subarray and solve the problem in the following two phases. |
| 5 | +
|
| 6 | + Create a table profit[0..n-1] and initialize all values in it to 0. |
| 7 | +Traverse price[] from right to left and update profit[i] such that profit[i] stores maximum profit achievable from one transaction in subarray price[i..n-1] |
| 8 | +Traverse price[] from left to right and update profit[i] such that profit[i] stores maximum profit such that profit[i] contains maximum achievable profit from two transactions in subarray price[0..i]. |
| 9 | +Return profit[n-1] |
| 10 | +
|
| 11 | +
|
| 12 | +*/ |
| 13 | + |
| 14 | +// Approach 1: Naive approach |
| 15 | + |
| 16 | +// Time complexity: O(n) |
| 17 | + |
| 18 | +// Auxiliary space: O(n) |
| 19 | + |
| 20 | +{ |
| 21 | + function maxProfit(price, n) { |
| 22 | + // create profit array and initialize it as 0 |
| 23 | + let profit = new Array(n); |
| 24 | + for (let i = 0; i < n; i++) { |
| 25 | + profit[i] = 0; |
| 26 | + /* Get the maximum profit with |
| 27 | + only one transaction |
| 28 | + allowed. After this loop, |
| 29 | + profit[i] contains maximum |
| 30 | + profit from price[i..n-1] |
| 31 | + using at most one trans. */ |
| 32 | + |
| 33 | + let max_price = price[n - 1]; |
| 34 | + for (let i = n - 2; i >= 0; i--) { |
| 35 | + //max_price has maximum |
| 36 | + // of price[i..n-1] |
| 37 | + if (price[i] > max_price) max_price = price[i]; |
| 38 | + // We can get profit[i] by taking maximum of: |
| 39 | + // a) previous maximum, i.e., profit[i+1] |
| 40 | + // b) profit by buying at price[i] and selling at |
| 41 | + // max_price |
| 42 | + profit[i] = Math.max(profit[i + 1], max_price - price[i]); |
| 43 | + } |
| 44 | + |
| 45 | + // Get the maximum profit with |
| 46 | + // two transactions allowed |
| 47 | + // After this loop, profit[n-1] |
| 48 | + // contains the result |
| 49 | + |
| 50 | + let min_price = price[0]; |
| 51 | + for (let i = 1; i < n; i++) { |
| 52 | + // min_price is minimum price |
| 53 | + // in price[0..i] |
| 54 | + if (price[i] < min_price) min_price = price[i]; |
| 55 | + // Maximum profit is maximum of: |
| 56 | + // a) previous maximum, i.e., profit[i-1] |
| 57 | + // b) (Buy, Sell) at (min_price, price[i]) and add |
| 58 | + // profit of other trans, stored in profit[i] |
| 59 | + |
| 60 | + profit[i] = Math.max(profit[i - 1], profit[i] + (price[i] - min_price)); |
| 61 | + } |
| 62 | + } |
| 63 | + let result = profit[n - 1]; |
| 64 | + |
| 65 | + return result; |
| 66 | + } |
| 67 | + // Driver code |
| 68 | + let price = [2, 30, 15, 10, 8, 25, 80]; |
| 69 | + let n = price.length; |
| 70 | + |
| 71 | + console.log("Maximum Profit = " + maxProfit(price, n)); |
| 72 | +} |
| 73 | + |
| 74 | +// Approach 2: Recursive Approach |
| 75 | + |
| 76 | +// Time Complexity : O(2^N) |
| 77 | + |
| 78 | +// Auxiliary Space : O(N) |
| 79 | + |
| 80 | +{ |
| 81 | + function f(idx, buy, prices, cap, n) { |
| 82 | + if (cap == 0) { |
| 83 | + return 0; |
| 84 | + } |
| 85 | + |
| 86 | + if (idx == n) { |
| 87 | + return 0; |
| 88 | + } |
| 89 | + |
| 90 | + let profit = 0; |
| 91 | + |
| 92 | + // you can either buy or not buy |
| 93 | + if (buy == 0) { |
| 94 | + profit = Math.max( |
| 95 | + -prices[idx] + f(idx + 1, 1, prices, cap, n), |
| 96 | + f(idx + 1, 0, prices, cap, n), |
| 97 | + ); |
| 98 | + } else { |
| 99 | + profit = Math.max( |
| 100 | + prices[idx] + f(idx + 1, 0, prices, cap - 1, n), |
| 101 | + f(idx + 1, 1, prices, cap, n), |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + return profit; |
| 106 | + } |
| 107 | + |
| 108 | + function maxtwobuysell(prices, n) { |
| 109 | + return f(0, 0, prices, 2, n); |
| 110 | + } |
| 111 | + |
| 112 | + const arr = [2, 30, 15, 10, 8, 25, 80]; |
| 113 | + const size = arr.length; |
| 114 | + console.log(maxtwobuysell(arr, size)); |
| 115 | +} |
0 commit comments