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:
Imagine you are looking at a history of daily prices for a single stock and want to figure out the absolute best days to buy and sell for maximum profit. The brute force approach involves checking every possible combination of a buy day and a sell day to see which pair yields the highest gain.
Here's how the algorithm would work step-by-step:
def max_profit_brute_force(stock_prices):
maximum_profit = 0
# Iterate through every possible buy day to ensure we test all combinations
for buy_day_index in range(len(stock_prices)):
for sell_day_index in range(buy_day_index + 1, len(stock_prices)):
current_profit = stock_prices[sell_day_index] - stock_prices[buy_day_index]
# Update the peak profit whenever we discover a higher return
if current_profit > maximum_profit:
maximum_profit = current_profit
# Return the highest accumulated profit found across all pairs
return maximum_profitTo find the maximum profit from a series of daily prices, the optimal approach tracks the lowest price seen so far while simultaneously calculating the potential profit if we sold on the current day. By walking through the timeline just once and remembering the cheapest cost up to that moment, we can instantly spot the best deal without needing to compare every possible pair of buying and selling days.
Here's how the algorithm would work step-by-step:
def max_profit(stock_prices):
if not stock_prices:
return 0
# We track the minimum price seen so far to know the best past day to buy.
lowest_price_so_far = stock_prices[0]
max_recorded_profit = 0
for current_price in stock_prices:
# If we encounter a cheaper buying price, update our baseline.
if current_price < lowest_price_so_far:
lowest_price_so_far = current_price
# Calculate profit for selling today using the cheapest past price.
current_potential_profit = current_price - lowest_price_so_far
# Keep track of the highest profit encountered across the timeline.
if current_potential_profit > max_recorded_profit:
max_recorded_profit = current_potential_profit
return max_recorded_profit| Case | How to Handle |
|---|---|
| Empty or null input array of prices | Return zero immediately since no transactions can possibly be made without any days. |
| Array with only a single element | Return zero because a stock must be bought and sold on two different days. |
| Prices in strictly descending order throughout the array | Track the running minimum price and keep the profit at zero since prices always drop. |
| Prices in strictly ascending order throughout the array | Continuously update the minimum price and calculate the maximum difference between the last element and the first. |
| Array with all identical stock prices | Keep the maximum profit at zero because buying and selling at the same price yields no gain. |
| Extremely large arrays with millions of elements | Use a single-pass O(N) time and O(1) space linear scan to scale efficiently without memory or timeout issues. |
| Extreme boundary values and large price integers | Use standard integer types since subtraction of stock prices will not exceed standard integer limits. |