Best Time to Buy and Sell Stock
Find the maximum profit from buying and selling a stock once
You are given an array prices where prices[i] is the price of a given stock on the ith day. You may buy on one day and sell on another day later. Return the maximum profit you can achieve. If no profit is possible, return 0.
R — READ
READ
What constraints apply? Can I short-sell? Must I hold before selling?
Buy on one day, sell on a later day (not before). Find maximum profit, or 0 if no profit.
Do you clarify that sell must come after buy?
Allowing selling before buying, or selling on the same day.
Input: prices = [7,1,5,3,6,4]
Output: 5 (buy at 1, sell at 6)
Input: prices = [7,6,4,3,1]
Output: 0 (prices only fall, no profit)
O — OBSERVE
OBSERVE
What's the key insight?
For each day, you want to sell at the highest price you can reach. That means: track the minimum price seen so far, then compute profit = current price − minimum.
Can you recognize the need to track the minimum dynamically?
Checking all pairs (O(n²)) when a single pass works.
Example: [7, 1, 5, 3, 6, 4]
• Day 0: price=7, min=7, profit=0
• Day 1: price=1, min=1, profit=0
• Day 2: price=5, min=1, profit=4
• Day 3: price=3, min=1, profit=2
• Day 4: price=6, min=1, profit=5 ← max
• Day 5: price=4, min=1, profit=3
P — PSEUDOCODE
PSEUDOCODE
What's my plan?
Track minimum price seen. For each price, compute potential profit. Keep the maximum.
Clear logic without code-like syntax.
Pseudocode that's too vague or already in Python.
1. Initialize min_price = first price
2. Initialize max_profit = 0
3. For each price starting from day 1:
a. Compute profit = price − min_price
b. Update max_profit if profit is larger
c. Update min_price if current price is smaller
4. Return max_profit
I — IMPLEMENT
IMPLEMENT
How do I code this cleanly?
Initialize correctly. Update min_price and max_profit in the right order.
Correct variable updates and loop logic.
Updating min_price before computing profit (or vice versa).
def maxProfit(prices):
"""
Find the maximum profit from one buy-sell transaction.
Args:
prices: List of integers representing daily stock prices
Returns:
Maximum profit possible, or 0 if no profit
"""
if not prices or len(prices) < 2:
return 0
min_price = prices[0]
max_profit = 0
for price in prices[1:]:
profit = price - min_price
max_profit = max(max_profit, profit)
min_price = min(min_price, price)
return max_profit
T — TEST
TEST
Does my code work on all cases?
Test: normal profit, no profit, single element, two elements, all increases/decreases.
Edge case handling and correctness.
Not testing edge cases like short arrays.
Test 1: [7,1,5,3,6,4] → 5 ✓
Test 2: [7,6,4,3,1] → 0 (falling prices) ✓
Test 3: [1] → 0 (single element) ✓
Test 4: [2,4] → 2 (simple profit) ✓
Test 5: [2,1,2,0,1] → 1 ✓
Test 6: [1,2,3,4,5] → 4 (buy at 1, sell at 5) ✓
A — ANALYZE
ANALYZE
What's the complexity?
Single pass through prices. Each operation (comparison, update) is O(1).
Correct complexity reasoning.
Counting nested loops or forgetting that min/max are O(1).
Why: We iterate through prices once (O(n)). For each price, we do constant-time comparisons (O(1)). We store only a few variables: min_price, max_profit.
Brute force: Checking all pairs is O(n²). This single-pass approach is much better.