← Greedy

Pattern Recognition Drill

#71 — Best time to buy/sell stock

Easy Greedy

The Problem

Find maximum profit from one buy and one sell (buy before sell).

What approach would you use?

Think about it before scrolling down.

Key Signals

Greedy

Track min_price. At each day: max_profit = max(max_profit, price - min_price). O(n).

Alt: Dynamic Programming

DP formulation works but is overkill. Greedy with running min is simpler.

Common Trap

Brute force checking all (buy, sell) pairs is O(n²). The running minimum insight makes it O(n).

← #70 #72 →