Skip to content

Practice Best Time to Buy and Sell Stock

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#121
LeetCode Problem

Best Time to Buy and Sell Stock

Problem Statement

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.

Source: Best Time to Buy and Sell Stock on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
prices = [7,1,5,3,6,4]
Output
5

# Example 2

Input
prices = [7,6,4,3,1]
Output
0

Constraints

1 <= prices.length <= 10^5
0 <= prices[i] <= 10^4

How to solve Best Time to Buy and Sell Stock

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

The best sale on any given day pairs it with the lowest price seen on an earlier day. One pass that carries the running minimum price and the running best profit therefore answers the question.

Why brute force is not enough

Try every buy day paired with every later sell day and keep the largest difference. That is O(n^2) pairs, and with prices.length up to 10^5 it is about 5 * 10^9 comparisons.

Approach

  1. Keep the lowest price seen so far, initialized to the first day's price, and the best profit, initialized to 0.
  2. For each later day, compute today's price minus the lowest price so far and raise the best profit if this is larger. Invariant: the best profit equals the maximum achievable by selling on or before today.
  3. Then lower the running minimum if today's price is below it. Computing the profit before updating the minimum keeps the buy day strictly earlier than the sell day.
  4. Return the best profit; it stays 0 when prices never rise, which is the required answer for no profitable trade.
  5. If asked, note that this is the maximum subarray sum of the day-to-day price differences, solved with the same running-sum idea.

Walkthrough of Example 1

prices = [7,1,5,3,6,4] → 5

  1. Lowest price starts at 7 and best profit at 0. Day with price 1: 1 - 7 is negative, best stays 0; lowest becomes 1.
  2. Price 5: 5 - 1 = 4, best becomes 4.
  3. Price 3: 3 - 1 = 2, best stays 4; lowest stays 1.
  4. Price 6: 6 - 1 = 5, best becomes 5.
  5. Price 4: 4 - 1 = 3, best stays 5. Return 5, buying at 1 and selling at 6.

Time and space

Time O(n) · Space O(1). n is prices.length; each price is examined once and only two running numbers are stored.

Interview pitfalls

  • Taking the overall maximum minus the overall minimum without respecting order; for 7, 6, 4, 3, 1 that gives 6 when the answer is 0.
  • Updating the minimum before computing the day's profit; here it merely yields a zero profit, but it models selling on the buy day and will bite in variants with fees.
  • Initializing the best profit to a negative sentinel and returning it; the problem requires 0 when no trade helps.
  • Forgetting that a single-day input returns 0.
  • Allowing the sell day to equal the buy day in the nested-loop version, which is both wrong in spirit and still too slow at 10^5.

Follow-up questions interviewers ask

  • Allow unlimited transactions, at most two, or at most k; which of these still admit a greedy and which need dynamic programming?
  • Add a cooldown day after each sale or a fixed fee per transaction.
  • Prices arrive as a stream; maintain the answer online and also report the buy and sell days.

Related LeetCode Problems

Practice more problems similar to Best Time to Buy and Sell Stock.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.