Advertisement

Best Time to Buy and Sell Stock with Transaction Fee - LeetCode 714 Solution

Best Time to Buy and Sell Stock with Transaction Fee - Complete Solution Guide

Best Time to Buy and Sell Stock with Transaction Fee is LeetCode problem 714, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an array prices where prices[i] is the price of a given stock on the i th day, and an integer fee representing a transaction fee. 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. Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again). The transaction fee is only charged once for each stock purchase and sale. Example

Detailed Explanation

The problem asks us to find the maximum profit that can be obtained by buying and selling stocks with a transaction fee. We are given an array `prices` representing the stock prices on each day and an integer `fee` representing the transaction fee for each buy and sell operation. We can perform multiple transactions, but we can't hold multiple stocks simultaneously (i.e., we must sell before buying again). The goal is to maximize the total profit after accounting for the transaction fees.

Solution Approach

The solution uses a dynamic programming approach with two variables, `cash` and `hold`. `cash` represents the maximum profit if we don't hold any stock at the end of the day, and `hold` represents the maximum profit if we hold a stock at the end of the day. We iterate through the `prices` array, updating `cash` and `hold` at each step. At each day, we consider two options for each state: either keep the state from the previous day or transition to the other state by buying or selling.

Step-by-Step Algorithm

  1. Step 1: Initialize `cash` to 0 (no profit initially) and `hold` to `-prices[0]` (the profit/loss if we buy on the first day).
  2. Step 2: Iterate through the `prices` array from the second day (index 1).
  3. Step 3: For each day `i`, calculate the new `cash` value. This is the maximum of the previous `cash` value (no action) and the `hold` value plus the current price minus the fee (selling the stock). Crucially, we store the *previous* cash value before we update it.
  4. Step 4: For each day `i`, calculate the new `hold` value. This is the maximum of the previous `hold` value (no action) and the previous `cash` value minus the current price (buying the stock). We use the *previous* cash value because the current cash value reflects whether we sold *today*, but our hold state represents whether we would buy today.
  5. Step 5: Update `cash` and `hold` variables with the calculated values.
  6. Step 6: After iterating through all the prices, the maximum profit will be stored in `cash` because at the end, we prefer not holding the stock.

Key Insights

  • Insight 1: The problem can be solved using dynamic programming, where we keep track of two states: `cash` (maximum profit when not holding a stock) and `hold` (maximum profit when holding a stock).
  • Insight 2: Each day, we can either continue holding the stock or sell it (if holding). Similarly, we can either continue not holding or buy the stock (if not holding).
  • Insight 3: The transaction fee is only applied when selling the stock, so it affects the `cash` state when transitioning from `hold` to `cash`.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Dynamic Programming, Greedy.

Companies

Asked at: Citadel.