You are given an array prices
where prices[i]
is the price of a given stock on the ith
day.
Find the maximum profit you can achieve. You may complete at most two transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: prices = [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
Example 2:
Input: prices = [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
Constraints:
1 <= prices.length <= 105
0 <= prices[i] <= 105
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 problem wants us to find the maximum profit we can make by buying and selling a stock at most two times. The brute force way is to try every single possible pair of buy and sell days, and see which combination gives us the most profit.
Here's how the algorithm would work step-by-step:
def max_profit_two_transactions_brute_force(prices):
number_of_days = len(prices)
max_overall_profit = 0
# Consider no transactions.
# Consider all single transactions.
for buy_day_one in range(number_of_days):
for sell_day_one in range(buy_day_one + 1, number_of_days):
profit_one = prices[sell_day_one] - prices[buy_day_one]
# If the first transaction results in a loss, continue to the next one
if profit_one <= 0:
continue
max_overall_profit = max(max_overall_profit, profit_one)
# Consider all pairs of transactions.
for buy_day_two in range(sell_day_one + 1, number_of_days):
for sell_day_two in range(buy_day_two + 1, number_of_days):
# Calculate profit from the second transaction.
profit_two = prices[sell_day_two] - prices[buy_day_two]
# Total profit from both transactions.
total_profit = profit_one + profit_two
max_overall_profit = max(max_overall_profit, total_profit)
return max_overall_profit
This problem wants us to find the maximum profit we can make by buying and selling a stock at most twice. The clever approach is to break the problem into smaller, manageable parts by figuring out the best buy/sell points separately and then combining the best results.
Here's how the algorithm would work step-by-step:
def max_profit(prices):
number_of_days = len(prices)
if number_of_days < 2:
return 0
left_profit = [0] * number_of_days
min_price_so_far = prices[0]
# Calculate max profit if selling on each day with one transaction
for day in range(1, number_of_days):
min_price_so_far = min(min_price_so_far, prices[day])
left_profit[day] = max(left_profit[day - 1], prices[day] - min_price_so_far)
right_profit = [0] * number_of_days
max_price_so_far = prices[number_of_days - 1]
# Calculate max profit if buying on each day with one transaction, backwards
for day in range(number_of_days - 2, -1, -1):
max_price_so_far = max(max_price_so_far, prices[day])
right_profit[day] = max(right_profit[day + 1], max_price_so_far - prices[day])
max_total_profit = 0
#Find best split point for the two transactions
for day in range(number_of_days):
max_total_profit = max(max_total_profit, left_profit[day] + right_profit[day])
return max_total_profit
Case | How to Handle |
---|---|
Empty price array | Return 0 as no profit can be made with no prices. |
Array with only one price | Return 0 as buying and selling on the same day yields no profit. |
Prices are monotonically decreasing (always losing money) | The algorithm should return 0 as no profitable transaction is possible. |
Prices are monotonically increasing (always making money) | The algorithm should correctly calculate the maximum profit by buying at the lowest price and selling at the highest twice, if possible. |
Large price fluctuations leading to integer overflow | Use `long` or a similar data type to store profit to prevent overflow. |
Prices include negative values or zero values | The algorithm should work correctly as negative prices are not inherently problematic, but they must be handled correctly during profit calculations. |
Maximum number of allowed transactions is zero | Return 0, since no transactions are possible and hence no profit. |
All prices are the same | Return 0, because no transaction can yield profit |