Monotonic StackStack Patterns

The Monotonic Stack pattern applies when you need the Monotonic Stack technique within the Stack Patterns pattern. Its time complexity is O(n) and space complexity O(n). It is used in 9 problems on Thita, including Daily Temperatures, Final Prices With a Special Discount in a Shop and Find the Most Competitive Subsequence. Common variations are Largest Rectangle in Histogram and Valid Parentheses Matching.

Master monotonic stack for next greater element, daily temperatures, and stock span problems.

Monotonic Stack is one of the 6 subpatterns of the Stack Patterns pattern, which covers parentheses matching, monotonic stack, histogram, and expression evaluation. The whole pattern is about 2.5 hours of study. This subpattern is a core technique: expect it to come up directly in interviews.

What Monotonic Stack covers

Master stack-based algorithms including valid parentheses, monotonic stack for next greater element, expression evaluation, and histogram problems. Problems in this subpattern are usually searched for as monotonic stack, next greater element, daily temperatures, stock span, leetcode 496, leetcode 739.

How to practise Monotonic Stack on Thita.ai

Read the theory for Monotonic Stack, then work the problems attached to it in the browser editor. Your solution runs against the problem's test cases, and the AI coach offers a hint about the technique you are missing rather than a finished solution. Progress is tracked per subpattern, so the Stack Patterns tracker shows this one as covered once you have solved its problems.

Other subpatterns in Stack Patterns

Related DSA patterns

Where to go next

Monotonic Stack is one lesson in a 16-pattern DSA course. If you are preparing end to end, work the interview-critical patterns first and use the pattern sheet as the checklist; if you are here for one technique, the Stack Patterns guide is the shortest path back to the rest of it.

Preparing this lesson and its course outline.