Stacks & Monotonic Stacks

Stacks are last in, first out. Learn monotonic stacks for next greater element and histogram problems.

40 lessons
121 min

Lessons

1. Intro

The LIFO principle

2m

2. Vocabulary - Stack

Last in, first out

3m

3. Stack Visualization

See it work

2m

4. Stack Implementation

Array-backed

3m

5. Where Stacks Shine

Common applications

2m

6. The Matching Problem

Nested structures

3m

7. Problem - Valid Parentheses

Classic stack problem

3m1 problems

8. Valid Parentheses - Algorithm

Push and match

3m

9. Valid Parentheses - Implementation

Complete solution

4m1 problems

10. Lessons from Valid Parentheses

core idea

2m

11. Quiz: Stack Basics

LIFO behavior

2m1 problems

12. A Harder Problem

Beyond matching

3m

13. Vocabulary - Monotonic Stack

Ordered elements

3m

14. Why Monotonic Works

The core idea

4m

15. Monotonic Stack Visualization

Step by step

4m

16. Problem - Next Greater Element I

Monotonic stack intro

3m1 problems

17. Next Greater - Algorithm

Stack + hash map

3m

18. Next Greater - Implementation

Complete solution

4m1 problems

19. Lessons from Next Greater

Pattern recognition

2m

20. Quiz: Monotonic Stack

Pop condition

2m1 problems

21. Problem - Daily Temperatures

Next greater with indices

3m1 problems

22. Daily Temperatures - The Idea

Store indices

3m

23. Daily Temperatures - Implementation

Complete solution

4m1 problems

24. Lessons from Daily Temperatures

Index tracking

2m

25. A Famous Problem

Histogram rectangles

3m1 problems

26. Histogram - Key Observation

Extension limits

3m

27. Histogram - The Approach

When to calculate

4m

28. Histogram - Visualization

Trace the algorithm

5m

29. Histogram - Why It Works

Correctness argument

3m

30. Histogram - Implementation

Complete solution

5m1 problems

31. Lessons from Histogram

Advanced pattern

3m

32. Quiz: Histogram

Boundary calculation

2m1 problems

33. Monotonic Stack Variants

Increasing vs decreasing

3m

34. Previous vs Next

Direction matters

3m

35. Recognizing Stack Problems

Pattern hints

2m

36. Problem - Trapping Rain Water

Advanced application

3m1 problems

37. Trapping Water - Stack Approach

Layer by layer

4m

38. Trapping Water - Implementation

Complete solution

4m1 problems

39. Quiz: Stack Problem Types

Choosing approach

2m1 problems

40. Section Recap

What we learned

3m

Practice Problems

1.

Classic bracket matching problem that teaches basic stack operations for finding the longest valid bracket subsequence.

2.
Move BracketsCodeforceseasy

Teaches bracket balance counting using stack-based thinking to determine minimum moves for a regular bracket sequence.

3.
Bracket SubsequenceCodeforceseasy

Reinforces greedy stack approach for extracting regular bracket subsequences of specified length.

4.

Teaches using stacks to characterize bracket sequences and count valid concatenations via sequence signatures.

5.
Minimal StringCodeforcesmedium

Excellent problem combining stack with greedy strategy to build lexicographically minimal strings.

6.

Classic stack application for finding longest valid bracket sequences with counting. Teaches advanced stack indexing.

7.
Mike and FeetCodeforcesmedium

Fundamental monotonic stack problem for finding the range where each element is minimum. Essential optimization pattern.

8.

Combines monotonic stack with LIS concepts. Teaches maintaining multiple stacks for optimal color assignment.

9.

Teaches simulation with stacks for prefix removal operations, combining pattern recognition with bracket matching.

10.

Constructive problem requiring generation of multiple distinct regular bracket sequences.

11.
Trains and StatisticCodeforceshard

Advanced monotonic stack optimization for DP transitions. Efficiently find range maximums during state computation.

12.
Nikita and StackCodeforceshard

Advanced problem involving stack operation queries across time. Teaches persistent data structure concepts.

13.
Valid ParenthesesLeetCodeeasy

Perfect introduction to stack data structure; teaches the fundamental LIFO principle for matching paired elements.

14.
Daily TemperaturesLeetCodemedium

Most popular monotonic stack problem; combines next greater element with distance calculation.

15.

The gold standard for monotonic stack mastery; requires finding both previous and next smaller elements.

16.

Can be solved with two pointers, DP, or monotonic stack. The stack solution processes elements layer by layer.

17.

Teaches contribution technique with monotonic stacks. Each element contributes to multiple subarrays based on boundaries.

18.

Teaches greedy monotonic stack for lexicographical ordering; combines frequency counting with stack manipulation.

19.
132 PatternLeetCodemedium

Non-obvious monotonic stack application requiring reverse traversal; teaches maintaining candidates while searching.

20.

Requires finding the second greater element using two stacks; advanced extension of classic next greater pattern.

Ready to start learning?

Access all 40 lessons with interactive content and progress tracking.