LeetCode 121 · Easy

Best Time to Buy and Sell Stock Explained (LeetCode 121)

Updated Nov 27, 2026 · 3 min read

Walk the prices once and keep the cheapest price you've seen so far. On each day, today's price minus that cheapest is the best sale you could make today, so keep the largest of those. It runs in O(n) time and O(1) space, and it returns 0 when prices only fall.

Watch onYouTube

Key points

  • ●Checking every pair of days is O(n²): 66,430 pairs for a year of prices.
  • ●For any sell day, the best buy day is the cheapest earlier day.
  • ●Two variables are enough: cheapest so far and best profit so far.
  • ●Start best profit at 0 so falling prices never give a negative answer.
Step-by-step visual sheet: Best Time to Buy and Sell Stock
The full visual sheet, made with Edithly.Open the interactive sheet

The riddle: Mo's mango stand

Mo sells mangoes, and he's written down six days of prices: 8, 3, 6, 2, 7, 4. He can buy once and sell once, and the sale has to come on a later day. What's the biggest profit he can make?

That's LeetCode 121, Best Time to Buy and Sell Stock. You get a list of daily prices, you pick one buy day and one later sell day, and you return the largest profit. If no trade makes money, you return 0.

For Mo the answer is 5: buy at 2 on day 4, sell at 7 on day 5.

The slow way: check every pair of days

The first idea is to try every buy day with every later sell day and keep the best difference. Six days give 15 pairs, which is fine. A year of prices gives 66,430 pairs, and the problem allows up to 100,000 prices. That's O(n²), and it's too slow.

The real waste is that a pair search learns nothing from the past. It compares day 5 against day 1, then day 2, then day 3, when only one of those earlier days could ever matter.

The idea: a sticky note with one price

If you're selling today, which earlier day should you have bought on? The cheapest one. Nothing else about the past changes the answer.

So Mo keeps a sticky note with the cheapest price so far. Each day he does two things:

  • Work out today's profit: today's price minus the cheapest.
  • If today's price is lower than the note, write the new price on it.

He also remembers the best profit he's seen. That's the whole algorithm: two numbers and one pass.

Trace it on Mo's prices

Day Price Cheapest before today Profit today Best so far
1 8 (none) (none) 0
2 3 8 -5 0
3 6 3 3 3
4 2 3 -1 3
5 7 2 5 5
6 4 2 2 5

On day 5 the note says 2, the price is 7, and the profit jumps to 5. Day 6 can't beat it. Step 5 of the visual sheet above runs the same six prices line by line.

The code in Python

def max_profit(prices):
    if not prices:
        return 0
    cheapest = prices[0]
    best_profit = 0
    for price in prices[1:]:
        current_profit = price - cheapest
        best_profit = max(best_profit, current_profit)
        cheapest = min(cheapest, price)
    return best_profit

Run it on [8, 3, 6, 2, 7, 4] and you get 5. Notice the order inside the loop: you test the sale first, then update the cheapest price. That keeps the buy day strictly before the sell day, which is the rule the sheet's step 4 points out.

Complexity

  • Time: O(n), each price is looked at once.
  • Space: O(1), just cheapest and best_profit.

You go from checking every pair to a single scan by remembering one price.

The trap to watch for

Don't grab the lowest and highest prices in the whole list. In Mo's list the highest price, 8, comes first and the lowest, 2, comes later, so 8 minus 2 is a trade you can't make. The scan avoids this on its own because it only ever compares today against days that already happened.

The other trap is the starting value. If you start best_profit at a negative number, a list like [7, 6, 4, 3, 1] returns a loss. Starting at 0 means "don't trade", and that's the right answer when prices only fall.

Frequently asked questions

Why can't you just subtract the lowest price from the highest?

Because the lowest price might come after the highest one, and you have to buy before you sell. In [8, 3, 6, 2, 7, 4] the highest is 8 on day 1 and the lowest is 2 on day 4, but 8 minus 2 isn't a legal trade. The real answer is 5.

What is the time complexity of Best Time to Buy and Sell Stock?

O(n) time, because you look at each price once, and O(1) space, because you only store the cheapest price and the best profit. The brute force over every pair is O(n²).

What should the function return if prices only go down?

0. You're allowed to skip the trade, and since best profit starts at 0 and every sale would lose money, it never goes up. For [7, 6, 4, 3, 1] the answer is 0.

Is this problem related to Kadane's algorithm?

Yes. Both keep a tiny summary of the past while scanning once. Here the summary is the cheapest earlier price; in Maximum Subarray it's the best running total ending at the current number.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free