|
| 1 | +package LeetCodeJava.DynamicProgramming; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/ |
| 4 | +/** |
| 5 | + * 714. Best Time to Buy and Sell Stock with Transaction Fee |
| 6 | + * Medium |
| 7 | + * Topics |
| 8 | + * Companies |
| 9 | + * Hint |
| 10 | + * 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. |
| 11 | + * |
| 12 | + * 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. |
| 13 | + * |
| 14 | + * Note: |
| 15 | + * |
| 16 | + * You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again). |
| 17 | + * The transaction fee is only charged once for each stock purchase and sale. |
| 18 | + * |
| 19 | + * |
| 20 | + * Example 1: |
| 21 | + * |
| 22 | + * Input: prices = [1,3,2,8,4,9], fee = 2 |
| 23 | + * Output: 8 |
| 24 | + * Explanation: The maximum profit can be achieved by: |
| 25 | + * - Buying at prices[0] = 1 |
| 26 | + * - Selling at prices[3] = 8 |
| 27 | + * - Buying at prices[4] = 4 |
| 28 | + * - Selling at prices[5] = 9 |
| 29 | + * The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8. |
| 30 | + * Example 2: |
| 31 | + * |
| 32 | + * Input: prices = [1,3,7,5,10,3], fee = 3 |
| 33 | + * Output: 6 |
| 34 | + * |
| 35 | + * |
| 36 | + * Constraints: |
| 37 | + * |
| 38 | + * 1 <= prices.length <= 5 * 104 |
| 39 | + * 1 <= prices[i] < 5 * 104 |
| 40 | + * 0 <= fee < 5 * 104 |
| 41 | + * |
| 42 | + * |
| 43 | + */ |
| 44 | +public class BestTimeToBuyAndSellStockWithTransactionFee { |
| 45 | + |
| 46 | + // V0 |
| 47 | +// public int maxProfit(int[] prices, int fee) { |
| 48 | +// |
| 49 | +// } |
| 50 | + |
| 51 | + // V1 |
| 52 | + // https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/solutions/3667440/beats-100-c-java-python-beginner-friendl-rpgh/ |
| 53 | + public int maxProfit_1(int[] prices, int fee) { |
| 54 | + int buy = Integer.MIN_VALUE; |
| 55 | + int sell = 0; |
| 56 | + |
| 57 | + for (int price : prices) { |
| 58 | + buy = Math.max(buy, sell - price); |
| 59 | + sell = Math.max(sell, buy + price - fee); |
| 60 | + } |
| 61 | + |
| 62 | + return sell; |
| 63 | + } |
| 64 | + |
| 65 | + // V2 |
| 66 | + // https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/solutions/1112088/js-python-java-c-very-simple-state-machi-f4zp/ |
| 67 | + public int maxProfit_2(int[] P, int F) { |
| 68 | + int len = P.length, buying = 0, selling = -P[0]; |
| 69 | + for (int i = 1; i < len; i++) { |
| 70 | + buying = Math.max(buying, selling + P[i] - F); |
| 71 | + selling = Math.max(selling, buying - P[i]); |
| 72 | + } |
| 73 | + return buying; |
| 74 | + } |
| 75 | + |
| 76 | + // V3 |
| 77 | + // https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/solutions/1600084/dp-state-machine-top-down-to-bottom-up-w-vdzi/ |
| 78 | + |
| 79 | + // V4 |
| 80 | + // IDEA: DP |
| 81 | + // https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/solutions/3667495/beats-100-video-java-c-python-by-jeevank-uyrl/ |
| 82 | + public int maxProfit_4(int[] prices, int fee) { |
| 83 | + int free = 0; |
| 84 | + int hold = -prices[0]; |
| 85 | + for (int i : prices) { |
| 86 | + int tmp = hold; |
| 87 | + hold = Math.max(hold, free - i); |
| 88 | + free = Math.max(free, tmp + i - fee); |
| 89 | + } |
| 90 | + return free; |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments