MemotivaLeetCode Patterns Flashcards: Stack, Monotonic Stack, Valid Parentheses, Next Greater

What is a monotonic stack and what problems does it solve?

LeetCode Patterns Flashcards: Stack, Monotonic Stack, Valid Parentheses, Next Greater

Аудио-карточка · 0:28

Nortren·

What is a monotonic stack and what problems does it solve?

0:28

A monotonic stack is a stack where elements are kept in strictly increasing or decreasing order. When a new element violates the monotonic property, elements are popped until the property is restored. During each pop, you discover a relationship between the popped element, the new element, and the remaining stack top. Monotonic stacks solve "next greater element," "next smaller element," "largest rectangle in histogram," and "daily temperatures" in O of n time. Each element is pushed and popped at most once.
neetcode.io