← Sliding Window

Pattern Recognition Drill

#101 — Best Time to Buy and Sell Stock

Easy Sliding Window

The Problem

Given an array of stock prices by day, find the maximum profit from one buy-sell pair.

What approach would you use?

Think about it before scrolling down.

Key Signals

Sliding Window

Track running min price; at each step profit = price - min_so_far. O(n).

Alt: Two Pointers

Left at buy, right at sell. Move left when right finds lower price.

Common Trap

Don't try all O(n²) pairs. The running minimum makes it one-pass.

← #100