PROBLEM 60 OF 75 · CODE STUDIO

Best Time to Buy and Sell Stock

Given daily stock prices, choose one day to buy and a later day to sell. Return the largest possible non-negative profit; return zero when no profitable trade exists.

Know the exact contract

  • The local studio accepts non-negative integer prices. It also accepts an empty array and returns zero for it.
  • Buying must happen before selling. At most one buy-and-sell transaction is allowed; you cannot combine two separate gains.
EXAMPLE 1prices = [7, 1, 5, 3, 6, 4]→ 5

Buy at 1 and sell later at 6; their difference is 5.

EXAMPLE 2prices = [7, 6, 4, 3, 1]→ 0

Every later price is lower, so skipping the trade is best.

EXAMPLE 3prices = [2, 4, 1]→ 2

The later low price cannot erase the earlier valid gain from 2 to 4.

From repeated checks to one scan

SIMPLE APPROACH

Try every buy day with every later sell day and keep the largest positive difference. This checks the ordering rule directly, but costs O(n²) time and O(1) extra space.

IMPROVED APPROACH

Scan prices from left to right. Before treating today as a possible new low, compare today’s price with the cheapest earlier buy price. Update the best profit, then update the running minimum. This considers the best legal purchase for every possible sale day.

Why it works

Before processing day i, minimum is the cheapest price among days before i and best is the largest legal profit using sale days before i. Comparing today with minimum covers every trade ending today; updating minimum prepares the next day without allowing a same-day future sale.

Cost

Each price is read once, so the scan takes O(n) time and O(1) extra space. The pair-check baseline takes O(n²) time and O(1) extra space.

A tempting mistake

For [2, 4, 1], a low price of 1 appears after the profitable sale at 4. Resetting the best profit when the minimum falls would incorrectly return zero instead of two.

FOLLOW THE ACTUAL STATE

Which earlier price gives today a legal profit?

Each day can be a sale day. Compare it with the cheapest earlier buy, then update the minimum for tomorrow.

STEP 1 / 6

Day 0: price 7. Cheapest earlier: none yet.

Today’s profit: no legal sale yet · best so far: 0 · next minimum: 7

Day 0 becomes the first possible buy day. No sale is possible yet.
Result: 5
GUIDED CHECK · BEFORE YOU CODE

Can you prove the trade is legal?

This check gives feedback. Only a separate code pass records an independent solve.

1. What does the running minimum represent before today?
2. Why compare today’s price before updating the minimum?
3. For [2, 4, 1], what is the answer?
4. What if prices fall every day?
5. What is the scan’s cost?
INDEPENDENT SOLVE

Now write the code.

Write solve(data). data.prices is an array of non-negative integers. Return the largest profit from one buy before one sell, or zero. The checks include falling prices, short histories, empty input, and fresh histories.

Opening the code studio…

PROVE IT AGAIN TOMORROW

A fresh solve is the test.

Loading your saved attempts…

This lesson uses original explanations and examples. For additional practice, see the related LeetCode challenge.