Skip to content

Start typing to search patterns, problems and pages.

Stack

Think of a stack of plates. You only ever touch the top one, and the last plate you put down is the first one you pick up.

⏱ about 20 minutesNeeds: strings, arraysTime O(n)Space O(n)

2 · Watch

Stack in 60 seconds

Six brackets, nested inside each other, and each closer must meet the right opener. Stripping pairs and rescanning reads the string again and again.

3 · Now you drive

Keys:←→ stepSpace playR resetHomeEnd jump

Valid Parentheses

or try
Up to 12 brackets: ( ) [ ] { }. Anything else is ignored.
Start with an empty stack.Step 1 of 8. Starting out. 6 brackets to read. The stack is empty.
Step 1 of 8
solution.ts
function isValid(s: string): boolean {
const stack: string[] = [];
const pairs: Record<string, string> = { ")": "(", "]": "[", "}": "{" };
for (const ch of s) {
if (!(ch in pairs)) {
stack.push(ch);
} else if (stack.pop() !== pairs[ch]) {
return false;
}
}
return stack.length === 0;
}
▶ marks the line running now

4 · Your turn

0 of 7 problems solved

Start from this

// Items still waiting for their answer.
const stack: number[] = [];
for (const x of items) {
  // Pop every item that x settles.
  while (stack.length && shouldPop(stack.at(-1)!, x)) {
    resolve(stack.pop()!, x);
  }
  // x now waits for its own answer.
  stack.push(x);
}

Easy ways to go wrong

  • Popping from an empty stack. Check it first.
  • Forgetting what’s still on the stack when the loop ends.
  • Storing values when you’ll need indices later.
  • Pushing before popping. Settle the older items first, then push the new one.
  • Getting the order wrong. Decide up front whether the stack rises or falls from bottom to top.

Matching pairs

The newest open item is the first one to close.

  1. Easy01

    Valid Parentheses

    LeetCode 20 · Blind 75 · NeetCode 150

    The one you just watched. Write it from memory.

Parsing

Save what you have when a new level opens, and finish it when the level closes.

  1. Medium02

    Decode String

    LeetCode 394

    Push what you have at each [ and rebuild it at each ].

  2. Medium03

    Basic Calculator II

    LeetCode 227

    Hold terms on the stack. * and / fold into the top one at once.

Monotonic stack

Keep the stack in order. Each new value pops every item it answers.

  1. Medium04

    Daily Temperatures

    LeetCode 739 · NeetCode 150

    Same stack, but each new day pops every colder day it answers.

    Watch it run
  2. Hard05

    Largest Rectangle in Histogram

    LeetCode 84 · NeetCode 150

    Keep bars rising. A shorter bar ends every taller rectangle before it.

  3. Hard06

    Trapping Rain Water

    LeetCode 42 · NeetCode 150

    Same popping, but each pop fills one layer of water.

    Watch it run
  4. Hard07

    Sliding Window Maximum

    LeetCode 239 · NeetCode 150

    A queue this time: drop from the back like a stack, expire from the front.

UP NEXT · LESSON 6 OF 20Binary SearchHalve the search space every step.