Best Time to Buy and Sell Stock - LeetCode #121 Top-Interview 7/150

2 min

Introduction

Today we tackle LeetCode #121: Best Time to Buy and Sell Stock. This is the seventh challenge in the LeetCode Top Interview 150 set.

Problem Statement

You are given an array prices where prices[i] represents the price of a stock on day i. Your goal is to maximize profit by choosing one day to buy and a later day to sell. If no profit is possible, return 0.

Example 1:

Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 1 (price = 1), sell on day 4 (price = 6) → 6-1 = 5

Example 2:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: Prices only decrease, no profit possible.

Example 3:

Input: prices = [2,4,1]
Output: 2

Constraints:

  • 1prices.length1051 \leq prices.length \leq 10^5
  • 0prices[i]1040 \leq prices[i] \leq 10^4

Identified Test Cases

CaseInputOutputComment
Classic case[7,1,5,3,6,4]5Multiple ups and downs
Decreasing prices[7,6,4,3,1]0No profit
Only 2 days (up)[1,5]4Minimum case with profit
Only 2 days (down)[5,1]0Minimum case, no profit
Single day[10]0Impossible to trade
All prices equal[3,3,3,3]0No variation
Best profit at end[1,2,3,4,5]4Buy day 0, sell day 4

Approach & Analysis

Strategy: One Pass

The optimal solution is to traverse the array once, keeping track of the lowest price seen so far and calculating the maximum possible profit at each step. This pattern is known as greedy because we always make the best local decision.

export function maxProfit(prices: number[]): number {
  let maxProfit = 0
  let minPrevPrice = prices[0]

  for (let i = 1; i < prices.length; i++) {
    const maxProfitThatDay = prices[i] - minPrevPrice
    maxProfit = Math.max(maxProfit, maxProfitThatDay)
    minPrevPrice = Math.min(minPrevPrice, prices[i])
  }
  return maxProfit
}

Step-by-Step Explanation

  1. Initialize maxProfit to 0 and minPrevPrice to the first price.
  2. Iterate from the second day:
    • Calculate the profit if selling that day: prices[i] - minPrevPrice.
    • Update maxProfit if this profit is higher.
    • Update minPrevPrice if a lower price is found.
  3. At the end, return the maximum profit found.

Step-by-step example with [7,1,5,3,6,4]

DayPriceminPriceProfit if sold todaymaxProfitAction
077-0Start
111-0New minimum
2515-1=44Update profit
3313-1=24No improvement
4616-1=55New record!
5414-1=35No improvement

Complexity

  • Time: O(n)O(n)
  • Space: O(1)O(1)

Edge Cases & Considerations

CaseCode behavior
Empty array or nullprices.length < 2 → returns 0
Single elementSame handling → 0
Equal pricesmaxProfit never updates → 0
Minimum at endNever sells for profit → 0
Multiple peaksOnly the largest jump from a previous minimum

Reflections & Lessons

  • Greedy: Always keep the best local option (lowest price seen).
  • Minimal state: Only two values need to be tracked at all times.

Conclusion

This problem is a great example of how a greedy approach and minimal state analysis can lead to optimal and elegant solutions. The pattern of keeping the minimum and calculating the maximum profit appears in many array and optimization problems.

Additional Resources