You are given an array prices
where prices[i]
is the price of a given stock on the ith
day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0
.
Example 1:
Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Example 2:
Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transactions are done and the max profit = 0.
Constraints:
1 <= prices.length <= 105
0 <= prices[i] <= 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 way to solve this is to try every possible buy and sell combination. We check each possible day to buy the stock, and then for each of those days, we check every possible later day to sell the stock.
Here's how the algorithm would work step-by-step:
def best_time_to_buy_and_sell_stock_brute_force(prices):
maximum_profit = 0
# Iterate through all possible buying days
for buying_day in range(len(prices)):
# Iterate through all possible selling days after the buying day
for selling_day in range(buying_day + 1, len(prices)):
# Calculate the potential profit for this buy/sell combination
potential_profit = prices[selling_day] - prices[buying_day]
# Update maximum_profit if this profit is greater
if potential_profit > maximum_profit:
maximum_profit = potential_profit
return maximum_profit
The best strategy to maximize profit is to find the lowest price to buy and the highest price after that point to sell. We achieve this by keeping track of the lowest price we've seen so far and updating our maximum profit whenever we find a selling price that gives us a higher profit.
Here's how the algorithm would work step-by-step:
def max_profit(prices):
if not prices:
return 0
best_buying_price = prices[0]
maximum_profit = 0
for current_price in prices:
# If we find a lower buying price, update it.
if current_price < best_buying_price:
best_buying_price = current_price
# Otherwise, check to update max profit.
else:
potential_profit = current_price - best_buying_price
#Keep track of the largest profit seen so far
if potential_profit > maximum_profit:
maximum_profit = potential_profit
# Return the maximum profit found
return maximum_profit
Case | How to Handle |
---|---|
Empty input array | Return 0, as no profit can be made with no prices. |
Input array with only one element | Return 0, as buying and selling on the same day yields no profit. |
Input array with prices in strictly decreasing order | Return 0, as no profitable transaction is possible. |
Input array with all prices being the same | Return 0, as no change in price allows for any profit. |
Large input array (scalability) | The algorithm should use O(n) time complexity for optimal performance with large inputs. |
Input array contains very large price values leading to potential integer overflow | Use a data type that can accommodate larger values (e.g., long) or handle potential overflow explicitly. |
Input array with negative price values | The problem doesn't explicitly state that prices cannot be negative; algorithm should handle them correctly by calculating the maximum profit even with negative prices. |
Prices array contains zeros | The presence of zero in prices should not affect the logic as it would be treated as a regular price point. |