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.
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
Constraints:
1 <= prices.length <= 5 * 104
1 <= prices[i] < 5 * 104
0 <= fee < 5 * 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method explores all possible combinations of buying and selling the stock. We'll go through every potential transaction and figure out the profit to find the best one. It's like checking every single path to see which one makes the most money, even if it takes a while.
Here's how the algorithm would work step-by-step:
def max_profit_brute_force(prices, transaction_fee):
max_total_profit = 0
for buy_day in range(len(prices)):
for sell_day in range(buy_day + 1, len(prices)):
#We are considering all the days after the purchase to sell on
potential_profit = prices[sell_day] - prices[buy_day] - transaction_fee
if potential_profit > 0:
#Track best profit possible
max_total_profit = max(max_total_profit, potential_profit)
# Return 0 if no transactions are made, otherwise return the max profit
return max_total_profit
We want to find the most profit we can make by buying and selling stock, but each sale has a fee. The key idea is to track two things: how much money we have if we're holding stock and how much we have if we're not, updating these values as we go through the stock prices.
Here's how the algorithm would work step-by-step:
def max_profit_with_transaction_fee(prices, fee):
money_if_holding_stock = -float('inf')
money_if_not_holding_stock = 0
for price in prices:
# Decide whether to buy stock if not holding it.
previous_money_if_not_holding
= money_if_not_holding_stock
money_if_not_holding_stock = max(money_if_not_holding_stock,
money_if_holding_stock + price - fee)
# Decide whether to sell stock if holding it.
money_if_holding_stock = max(money_if_holding_stock,
previous_money_if_not_holding - price)
# Max profit is when not holding stock at the end.
return money_if_not_holding_stock
Case | How to Handle |
---|---|
Empty price array | Return 0 profit immediately since no transactions are possible. |
Single day (array with one element) | Return 0 profit because you cannot buy and sell on the same day. |
Prices are monotonically increasing | Calculate profit by buying on the first day and selling on the last, subtracting the transaction fee for each transaction. |
Prices are monotonically decreasing | Return 0 profit because no transaction will be profitable after considering the fee. |
Transaction fee is zero | Solve as a regular buy and sell stock problem without a fee. |
Transaction fee is larger than the price range | Return 0 profit since no transaction can be profitable. |
Integer overflow with large prices and many transactions | Use a larger data type like long to store the profit to avoid overflow. |
Prices array contains very large numbers | The DP solution should still function correctly as long as the intermediate calculations do not overflow; consider using larger datatypes if necessary. |