You are given an integer array prices where prices[i] is the price of a stock in dollars on the ith day, and an integer k.
You are allowed to make at most k transactions, where each transaction can be either of the following:
Normal transaction: Buy on day i, then sell on a later day j where i < j. You profit prices[j] - prices[i].
Short selling transaction: Sell on day i, then buy back on a later day j where i < j. You profit prices[i] - prices[j].
Note that you must complete each transaction before starting another. Additionally, you can't buy or sell on the same day you are selling or buying back as part of a previous transaction.
Return the maximum total profit you can earn by making at most k transactions.
Example 1:
Input: prices = [1,7,9,8,2], k = 2
Output: 14
Explanation:
We can make $14 of profit through 2 transactions:Example 2:
Input: prices = [12,16,19,19,8,1,19,13,9], k = 3
Output: 36
Explanation:
We can make $36 of profit through 3 transactions:Constraints:
2 <= prices.length <= 1031 <= prices[i] <= 1091 <= k <= prices.length / 2When 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 in this case means trying every single possible combination of buy and sell actions. We want to explore all possible trading strategies to find the one that gives us the maximum profit, no matter how inefficient.
Here's how the algorithm would work step-by-step:
def best_time_to_buy_and_sell_stock_v_brute_force(prices):
max_profit = 0
number_of_days = len(prices)
def calculate_max_profit(start_day, current_profit):
nonlocal max_profit
if start_day >= number_of_days:
max_profit = max(max_profit, current_profit)
return
# Explore the option of not making any transaction
max_profit = max(max_profit, current_profit)
for buying_day in range(start_day, number_of_days):
for selling_day in range(buying_day + 1, number_of_days):
profit = prices[selling_day] - prices[buying_day]
# Explore further transactions after this sell
calculate_max_profit(selling_day + 1, current_profit + profit)
# Begin exploring all possible transaction sequences
calculate_max_profit(0, 0)
return max_profitThe best strategy is to keep track of potential opportunities to profit. We aim to capture all positive price differences to maximize earnings, without actually buying and selling on the same day. Think of it as finding all the upswings and summing them up.
Here's how the algorithm would work step-by-step:
def max_profit(prices):
maximum_total_profit = 0
# Iterate through prices to find profitable transactions
for i in range(1, len(prices)):
# Check if the current day's price is higher
if prices[i] > prices[i - 1]:
# Add the difference to the total profit
maximum_total_profit += prices[i] - prices[i - 1]
return maximum_total_profit| Case | How to Handle |
|---|---|
| prices is null or empty | Return 0 immediately as no transaction is possible. |
| prices array contains only one element | Return 0 immediately as no transaction is possible. |
| prices array is very large (performance considerations) | Ensure algorithm has optimal time complexity, such as O(n), to avoid timeouts. |
| prices array contains all identical values | Return 0 as no profit can be made. |
| prices array contains only decreasing values | Return 0 as no profit can be made. |
| prices array contains negative values (invalid price) | Throw an IllegalArgumentException or handle it based on requirements. |
| prices array causes integer overflow when calculating profit | Use a larger data type like long to store profit. |
| The prices array contain consecutive increasing prices. | Sum the daily increases in stock price to find the profit. |