java.lang.Object
g0701_0800.s0714_best_time_to_buy_and_sell_stock_with_transaction_fee.Solution

public class Solution extends Object
714 - Best Time to Buy and Sell Stock with Transaction Fee.<p>Medium</p> <p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day, and an integer <code>fee</code> representing a transaction fee.</p> <p>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.</p> <p><strong>Note:</strong> You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> prices = [1,3,2,8,4,9], fee = 2</p> <p><strong>Output:</strong> 8</p> <p><strong>Explanation:</strong> The maximum profit can be achieved by:</p> <ul> <li> <p>Buying at prices[0] = 1</p> </li> <li> <p>Selling at prices[3] = 8</p> </li> <li> <p>Buying at prices[4] = 4</p> </li> <li> <p>Selling at prices[5] = 9</p> </li> </ul> <p>The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> prices = [1,3,7,5,10,3], fee = 3</p> <p><strong>Output:</strong> 6</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= prices.length <= 5 * 10<sup>4</sup></code></li> <li><code>1 <= prices[i] < 5 * 10<sup>4</sup></code></li> <li><code>0 <= fee < 5 * 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxProfit

      public int maxProfit(int[] prices, int fee)