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 <= 1050 <= prices[i] <= 104When 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:
This problem is about finding the biggest profit you can make by buying a stock on one day and selling it on a later day. The brute force approach simply checks every single possible pair of buy and sell days.
Here's how the algorithm would work step-by-step:
def best_time_to_buy_and_sell_stock_brute_force(stock_prices):
maximum_profit = 0
number_of_days = len(stock_prices)
# Iterate through each possible day to buy the stock
for buy_day_index in range(number_of_days):
# For the selected buy day, iterate through all subsequent days as potential sell days
for sell_day_index in range(buy_day_index + 1, number_of_days):
# Calculate the profit for this buy-sell pair
current_profit = stock_prices[sell_day_index] - stock_prices[buy_day_index]
# Update the maximum profit if the current profit is greater
if current_profit > maximum_profit:
maximum_profit = current_profit
return maximum_profitTo find the greatest possible profit, we track the lowest price seen so far and calculate the potential profit if we sold at the current price. We then keep the largest profit we've encountered.
Here's how the algorithm would work step-by-step:
def calculate_best_stock_profit(stock_prices):
minimum_buy_price_seen = float('inf')
maximum_profit_achieved = 0
for current_day_price in stock_prices:
# Update the lowest price if the current day's price is lower. This is our new best buying opportunity.
if current_day_price < minimum_buy_price_seen:
minimum_buy_price_seen = current_day_price
# Calculate potential profit by selling at the current price, having bought at the lowest seen so far.
potential_profit_today = current_day_price - minimum_buy_price_seen
# Update the maximum profit if the current day's potential profit is greater.
if potential_profit_today > maximum_profit_achieved:
maximum_profit_achieved = potential_profit_today
return maximum_profit_achieved| Case | How to Handle |
|---|---|
| Empty or null input array | The solution should gracefully handle this by returning 0 profit, as no transaction is possible. |
| Array with only one element | Since a buy and sell must be on different days, a single element array means no transaction is possible, resulting in 0 profit. |
| Array with prices strictly decreasing | The algorithm should correctly identify that no profit can be made and return 0. |
| Array with prices strictly increasing | The algorithm should identify the first day as the buy day and the last day as the sell day to maximize profit. |
| Array with all identical prices | No profit can be made, so the algorithm should correctly return 0. |
| Large input array | The solution's linear time complexity ensures efficient handling of large inputs without performance degradation. |
| Prices containing zero or negative values | The problem statement implies stock prices are non-negative, but if they were allowed, the algorithm would still function correctly by finding the maximum difference. |
| Maximum possible profit leading to integer overflow | Using a data type that can accommodate the maximum possible profit, such as a 64-bit integer, prevents overflow issues. |