Algorithms and Data Structures · Middle · Question 21 of 75

What is a monotonic stack used for?

Explanation

The stack keeps indices whose values are decreasing. When a bigger value arrives, it pops the smaller ones: the new value is their "next greater" element. Each index is pushed and popped once, so the pass is O(n).

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 21

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz