|
| 1 | +/* |
| 2 | +714. Best Time to Buy and Sell Stock with Transaction Fee |
| 3 | +
|
| 4 | +You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee. |
| 5 | +Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. |
| 6 | +Note: |
| 7 | +You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again). |
| 8 | +The transaction fee is only charged once for each stock purchase and sale. |
| 9 | + |
| 10 | +Example 1: |
| 11 | +Input: prices = [1,3,2,8,4,9], fee = 2 |
| 12 | +Output: 8 |
| 13 | +Explanation: The maximum profit can be achieved by: |
| 14 | +- Buying at prices[0] = 1 |
| 15 | +- Selling at prices[3] = 8 |
| 16 | +- Buying at prices[4] = 4 |
| 17 | +- Selling at prices[5] = 9 |
| 18 | +The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8. |
| 19 | +
|
| 20 | +Example 2: |
| 21 | +Input: prices = [1,3,7,5,10,3], fee = 3 |
| 22 | +Output: 6 |
| 23 | + |
| 24 | +Constraints: |
| 25 | +
|
| 26 | +1 <= prices.length <= 5 * 104 |
| 27 | +1 <= prices[i] < 5 * 104 |
| 28 | +0 <= fee < 5 * 104 |
| 29 | +*/ |
| 30 | +class Solution { |
| 31 | + static int memo[][] =new int[50001][2]; |
| 32 | + public int maxProfit(int[] prices, int fee) { |
| 33 | + //Recursion::) |
| 34 | + //return solveRec(prices,0,1,fee); |
| 35 | + // TLE |
| 36 | + |
| 37 | + // Arrays.stream(memo).forEach(a->Arrays.fill(a,-1)); |
| 38 | + // return solveMemo(prices,0,1,fee); |
| 39 | + // TLE |
| 40 | + |
| 41 | + //TC : O(n*2) // SC: O |
| 42 | + return solveTab(prices,fee); |
| 43 | + |
| 44 | + } |
| 45 | + public static int solveRec(int[] prices ,int indx,int buy,int fee){ |
| 46 | + //Base Case :: |
| 47 | + if(indx==prices.length) return 0; |
| 48 | + int profit=0; |
| 49 | + if(buy==1){ |
| 50 | + profit = -prices[indx]-fee+solveRec(prices,indx+1,0,fee); |
| 51 | + }else{ |
| 52 | + profit =prices[indx]+solveRec(prices,indx+1,1,fee); |
| 53 | + } |
| 54 | + int skip =solveRec(prices,indx+1,buy,fee); |
| 55 | + return Math.max(profit,skip); |
| 56 | + } |
| 57 | + public static int solveMemo(int[] prices,int indx,int buy,int fee){ |
| 58 | + //Base Case :: |
| 59 | + if(indx==prices.length) return 0; |
| 60 | + if(memo[indx][buy]!=-1){ |
| 61 | + return memo[indx][buy]; |
| 62 | + } |
| 63 | + int profit=0; |
| 64 | + if(buy==1){ |
| 65 | + profit = -prices[indx]-fee+solveMemo(prices,indx+1,0,fee); |
| 66 | + }else{ |
| 67 | + profit =prices[indx]+solveMemo(prices,indx+1,1,fee); |
| 68 | + } |
| 69 | + int skip =solveMemo(prices,indx+1,buy,fee); |
| 70 | + return Math.max(profit,skip); |
| 71 | + } |
| 72 | + public static int solveTab(int[] prices,int fee){ |
| 73 | + int n=prices.length; |
| 74 | + int tab[][] =new int[n+1][2]; |
| 75 | + Arrays.stream(tab).forEach(a->Arrays.fill(a,-1)); |
| 76 | + tab[n][0]=0; |
| 77 | + tab[n][1]=0; |
| 78 | + |
| 79 | + int profit=0; |
| 80 | + for(int i=n-1;i>=0;i--){ |
| 81 | + for(int j=0;j<=1;j++){ |
| 82 | + if(j==1){ |
| 83 | + profit = Math.max(prices[i]+tab[i+1][0],tab[i+1][1]); |
| 84 | + } |
| 85 | + if(j==0){ |
| 86 | + profit = Math.max(-prices[i]-fee+tab[i+1][1],tab[i+1][0]); |
| 87 | + } |
| 88 | + tab[i][j]=profit; |
| 89 | + } |
| 90 | + } |
| 91 | + return tab[0][0]; |
| 92 | + } |
| 93 | +} |
0 commit comments