You are given an array prices
where prices[i]
is the price of a given stock on the i<sup>th</sup>
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:
Example 1:
Input: prices = [1,3,2,8,4,9], fee = 2
Output: 8
Explanation: The maximum profit can be achieved by:
- Buying at prices[0] = 1
- Selling at prices[3] = 8
- Buying at prices[4] = 4
- Selling at prices[5] = 9
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
Example 2:
Input: prices = [1,3,7,5,10,3], fee = 3
Output: 6
How would you go about implementing a function to solve this problem? Consider various edge cases and optimize for both time and space complexity.
The most straightforward approach is to explore all possible transaction combinations to find the maximum profit. This would involve recursively trying every buy and sell point, which quickly becomes computationally expensive.
Code (Illustrative - Not Efficient):
def max_profit_recursive(prices, fee, index, holding):
if index == len(prices):
return 0
# Option 1: Do nothing
profit = max_profit_recursive(prices, fee, index + 1, holding)
if holding:
# Option 2: Sell if holding stock
profit = max(profit, prices[index] - fee + max_profit_recursive(prices, fee, index + 1, False))
else:
# Option 3: Buy if not holding stock
profit = max(profit, -prices[index] + max_profit_recursive(prices, fee, index + 1, True))
return profit
prices = [1, 3, 2, 8, 4, 9]
fee = 2
print(max_profit_recursive(prices, fee, 0, False))
Time Complexity: O(2^n), where n is the number of days (prices). Space Complexity: O(n) due to the recursion depth.
A more efficient solution uses dynamic programming or a greedy approach to track the maximum profit at each day, considering whether we are holding a stock or not.
Explanation:
cash
to 0 (maximum profit if we don't hold stock) and hold
to -prices[0]
(maximum profit if we hold stock on the first day).prices
array.cash
and hold
at each step:
cash = max(cash, hold + prices[i] - fee)
(either keep the previous cash, or sell the stock today).hold = max(hold, cash - prices[i])
(either keep holding the stock, or buy the stock today).cash
as the maximum profit.Code (Python):
def max_profit_optimal(prices, fee):
cash = 0 # Max profit if we don't hold stock
hold = -prices[0] # Max profit if we hold stock
for i in range(1, len(prices)):
cash = max(cash, hold + prices[i] - fee)
hold = max(hold, cash - prices[i])
return cash
prices = [1, 3, 2, 8, 4, 9]
fee = 2
print(max_profit_optimal(prices, fee))
Time Complexity: O(n), where n is the number of days (prices). Space Complexity: O(1), constant space.
prices
array is empty, the maximum profit is 0.The optimal solution leverages dynamic programming principles in a greedy manner to efficiently calculate the maximum profit. This approach provides a significant improvement over the brute-force method in terms of time complexity.