I've been working through algorithm problems, and I've discovered something that completely changes how I approach them: the "one sentence rule."

What's the One Sentence Rule?

Distill the entire algorithm's core logic into a single sentence that captures its essence.

When you can explain an algorithm in one sentence, you truly understand it. Everything else - the code, the edge cases, the optimizations - flows naturally from that one insight.

Why This Works

Most algorithm explanations dump code at you with step-by-step instructions. That's backwards. You memorize steps without understanding why they work.

The one sentence rule forces you to find the key insight first. Once you have that, the implementation becomes obvious.

Example: Daily Temperatures

The problem: Given an array of daily temperatures, return an array where each element is the number of days until a warmer temperature. If no warmer day exists, use 0.

Input: temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
Output: [1, 1, 4, 2, 1, 1, 0, 0]

Most explanations jump straight to "use a stack" and show you the code. But why a stack?

The One Sentence Rule

Each temperature is "valid as a waiting day until a warmer temperature appears."

That's the entire insight. A monotonic stack keeps track of temperatures in decreasing order, and when we find a warmer temperature, all cooler temperatures in the stack have found their answer.

When a warmer temperature arrives:

  1. Pop all cooler temperatures from the stack - They've been waiting, and now we found their answer
  2. Calculate the waiting time - For each popped temperature: current_index - popped_index
  3. Store the result - Put the waiting time in the answer array
  4. Push the current temperature - It now waits for something warmer

Any temperatures still in the stack at the end never found a warmer day, so they stay 0.

The Code

Once you have the rule, the code writes itself:

def daily_temperatures(temperatures):
    """
    Find days until warmer temperature for each day.
    
    Time: O(n) - each temp pushed and popped once
    Space: O(n) - stack stores at most n temperatures
    """
    result = [0] * len(temperatures)
    stack = []  # Stack of indices waiting for warmer temps
    
    for i, temp in enumerate(temperatures):
        # This temp answers all cooler temps waiting in stack
        while stack and temperatures[stack[-1]] < temp:
            prev_index = stack.pop()
            result[prev_index] = i - prev_index
        
        # This temp now waits for its answer
        stack.append(i)
    
    return result

Walking Through It

With [73, 74, 75, 71, 69, 72, 76, 73]:

Day 0 (73°): No previous days, start waiting

Stack: [0]
Result: [0, 0, 0, 0, 0, 0, 0, 0]

Day 1 (74°): Warmer than 73°

  1. Pop index 0 (73°)
  2. Calculate: 1 - 0 = 1 day wait
  3. Store result[0] = 1
  4. Push index 1
Stack: [1]
Result: [1, 0, 0, 0, 0, 0, 0, 0]

Day 2 (75°): Warmer than 74°

  1. Pop index 1 (74°)
  2. Calculate: 2 - 1 = 1 day wait
  3. Store result[1] = 1
  4. Push index 2
Stack: [2]
Result: [1, 1, 0, 0, 0, 0, 0, 0]

Day 3 (71°): Cooler than 75°, just wait

Stack: [2, 3]

Day 4 (69°): Cooler than 71°, just wait

Stack: [2, 3, 4]

Day 5 (72°): Warmer than 69° and 71°

  1. Pop index 4 (69°): result[4] = 5 - 4 = 1
  2. Pop index 3 (71°): result[3] = 5 - 3 = 2
  3. Push index 5
Stack: [2, 5]
Result: [1, 1, 0, 2, 1, 0, 0, 0]

Day 6 (76°): Warmer than 72° and 75°

  1. Pop index 5 (72°): result[5] = 6 - 5 = 1
  2. Pop index 2 (75°): result[2] = 6 - 2 = 4
  3. Push index 6
Stack: [6]
Result: [1, 1, 4, 2, 1, 1, 0, 0]

Day 7 (73°): Cooler than 76°, stays in stack (never answered)

Stack: [6, 7]
Final: [1, 1, 4, 2, 1, 1, 0, 0]

Each step follows the four-step process: pop cooler temps, calculate waiting time, store result, push current temp.

How I Apply This

When tackling a problem:

  1. Understand what we're asking - What's the core question?
  2. Find the insight - What makes it click?
  3. Write the rule - One clear sentence
  4. Let the code follow - Implementation flows from the rule

This works for many patterns:

  • Two pointers: "Move pointers based on comparison until meeting the target"
  • Binary search: "Eliminate half the search space based on a condition"
  • Sliding window: "Expand right, shrink left when invalid, track the maximum"

Why This Matters

Algorithm problems can feel like memorizing hundreds of disconnected solutions. The one sentence rule transforms it into pattern recognition.

Instead of thinking "this is problem #739, do X then Y then Z," you think "this needs backwards lookup of waiting elements - that's a stack."

It's understanding the principle, not memorizing the recipe.


Quick recap: The one sentence rule means capturing an algorithm's core insight in one sentence. From that insight, the implementation becomes clear. It transforms problem-solving from memorization to understanding.