Monotonic Stack Pattern: Template + 225 LeetCode Problems
Answer "what is the next greater element" for every position in one pass.
- 21 Easy
- 115 Medium
- 89 Hard
- O(n) time
What the monotonic stack pattern is
A monotonic stack is an ordinary stack with one invariant added: its contents are kept increasing or decreasing, and any element that would break the order is popped first. The pops are the algorithm. When a new value evicts the elements below it, that new value is by construction the nearest element to their right that is larger — so "next greater element" for every index falls out of a single left-to-right scan rather than an O(n²) search. The total cost stays linear because each index is pushed once and popped once, no matter how expensive an individual step looks. Pick the direction from the question: a decreasing stack surrenders its top to a larger arrival and answers next-greater queries, an increasing stack answers next-smaller, and storing indices rather than values is almost always right because the distance between them is usually part of the answer.
When to use it
- The question is about the nearest larger or smaller element in some direction.
- You are computing spans, widths or distances between a position and the first position that beats it.
- A histogram, a skyline, temperatures, or stock spans are involved.
- The brute force scans right from every index until it finds something bigger.
The monotonic stack template in Python
The shape, not a solution to any one problem. Adapt the condition and the summary being maintained; the skeleton stays the same across the 225 problems listed below.
def next_greater(nums):
result = [-1] * len(nums)
stack = [] # indices; their values stay decreasing
for i, value in enumerate(nums):
# every index this value evicts has just found its next greater element
while stack and nums[stack[-1]] < value:
result[stack.pop()] = value
stack.append(i)
return resultComplexity characteristics
- Time
- O(n)
- Auxiliary space
- O(n)
Each index is pushed exactly once and popped at most once, so the inner while loop does not multiply the outer loop: the total number of pops over the whole scan is bounded by n even though one step can pop many elements. The stack is the space, and it holds all n indices in the worst case — an input already sorted in the direction the stack maintains never pops anything until the end.
All 225 monotonic stack LeetCode problems
Every problem in the library the monotonic stack pattern applies to, grouped by LeetCode's own difficulty rating. 184 of the 225 carry a complete Python solution with a worked example and complexity analysis; the rest are listed for completeness, with the LeetCode Premium ones marked.
Easy (21)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 35 | Search Insert Position | Easy | Array, Binary Search |
| 69 | Sqrt(x) | Easy | Math, Binary Search |
| 222 | Count Complete Tree Nodes | Easy | Bit Manipulation, Tree, Binary Search +1 |
| 278 | First Bad Version | Easy | Binary Search, Interactive |
| 367 | Valid Perfect Square | Easy | Math, Binary Search |
| 374 | Guess Number Higher or Lower | Easy | Binary Search, Interactive |
| 441 | Arranging Coins | Easy | Math, Binary Search |
| 496 | Next Greater Element I | Easy | Stack, Array, Hash Table +1 |
| 704 | Binary Search | Easy | Array, Binary Search |
| 744 | Find Smallest Letter Greater Than Target | Easy | Array, Binary Search |
| 270 | Closest Binary Search Tree ValuePremium | Easy | Tree, Depth-First Search, Binary Search Tree +2 |
| 1064 | Fixed PointPremium | Easy | Array, Binary Search |
| 1150 | Check If a Number Is Majority Element in a Sorted ArrayPremium | Easy | Array, Binary Search |
| 1337 | The K Weakest Rows in a Matrix | Easy | Array, Binary Search, Matrix +2 |
| 1351 | Count Negative Numbers in a Sorted Matrix | Easy | Array, Binary Search, Matrix |
| 1475 | Final Prices With a Special Discount in a Shop | Easy | Stack, Array, Monotonic Stack |
| 1539 | Kth Missing Positive Number | Easy | Array, Binary Search |
| 1608 | Special Array With X Elements Greater Than or Equal X | Easy | Array, Binary Search, Sorting |
| 2089 | Find Target Indices After Sorting Array | Easy | Array, Binary Search, Sorting |
| 2389 | Longest Subsequence With Limited Sum | Easy | Greedy, Array, Binary Search +2 |
| 2529 | Maximum Count of Positive Integer and Negative Integer | Easy | Array, Binary Search, Counting |
Medium (115)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 33 | Search in Rotated Sorted Array | Medium | Array, Binary Search |
| 34 | Find First and Last Position of Element in Sorted Array | Medium | Array, Binary Search |
| 74 | Search a 2D Matrix | Medium | Array, Binary Search, Matrix |
| 81 | Search in Rotated Sorted Array II | Medium | Array, Binary Search |
| 153 | Find Minimum in Rotated Sorted Array | Medium | Array, Binary Search |
| 162 | Find Peak Element | Medium | Array, Binary Search |
| 240 | Search a 2D Matrix II | Medium | Array, Binary Search, Divide and Conquer +1 |
| 275 | H-Index II | Medium | Array, Binary Search |
| 300 | Longest Increasing Subsequence | Medium | Array, Binary Search, Dynamic Programming |
| 316 | Remove Duplicate Letters | Medium | Stack, Greedy, String +1 |
| 378 | Kth Smallest Element in a Sorted Matrix | Medium | Array, Binary Search, Matrix +2 |
| 400 | Nth Digit | Medium | Math, Binary Search |
| 402 | Remove K Digits | Medium | Stack, Greedy, String +1 |
| 436 | Find Right Interval | Medium | Array, Binary Search, Sorting |
| 456 | 132 Pattern | Medium | Stack, Array, Binary Search +2 |
| 497 | Random Point in Non-overlapping Rectangles | Medium | Reservoir Sampling, Array, Math +4 |
| 503 | Next Greater Element II | Medium | Stack, Array, Monotonic Stack |
| 528 | Random Pick with Weight | Medium | Array, Math, Binary Search +2 |
| 540 | Single Element in a Sorted Array | Medium | Array, Binary Search |
| 581 | Shortest Unsorted Continuous Subarray | Medium | Stack, Greedy, Array +3 |
| 654 | Maximum Binary Tree | Medium | Stack, Tree, Array +3 |
| 729 | My Calendar I | Medium | Design, Segment Tree, Array +2 |
| 731 | My Calendar II | Medium | Design, Segment Tree, Array +3 |
| 739 | Daily Temperatures | Medium | Stack, Array, Monotonic Stack |
| 853 | Car Fleet | Medium | Stack, Array, Sorting +1 |
| 875 | Koko Eating Bananas | Medium | Array, Binary Search |
| 901 | Online Stock Span | Medium | Stack, Design, Data Stream +1 |
| 918 | Maximum Sum Circular Subarray | Medium | Queue, Array, Divide and Conquer +2 |
| 1268 | Search Suggestions System | Medium | Trie, Array, String +3 |
| 255 | Verify Preorder Sequence in Binary Search TreePremium | Medium | Stack, Tree, Binary Search Tree +4 |
| 362 | Design Hit CounterPremium | Medium | Design, Queue, Array +2 |
| 702 | Search in a Sorted Array of Unknown SizePremium | Medium | Array, Binary Search, Interactive |
| 754 | Reach a Number | Medium | Math, Binary Search |
| 769 | Max Chunks To Make Sorted | Medium | Stack, Greedy, Array +2 |
| 852 | Peak Index in a Mountain Array | Medium | Array, Binary Search |
| 907 | Sum of Subarray Minimums | Medium | Stack, Array, Dynamic Programming +1 |
| 962 | Maximum Width Ramp | Medium | Stack, Array, Two Pointers +1 |
| 1008 | Construct Binary Search Tree from Preorder Traversal | Medium | Stack, Tree, Binary Search Tree +3 |
| 1011 | Capacity To Ship Packages Within D Days | Medium | Array, Binary Search |
| 1019 | Next Greater Node In Linked List | Medium | Stack, Array, Linked List +1 |
| 1060 | Missing Element in Sorted ArrayPremium | Medium | Array, Binary Search |
| 1062 | Longest Repeating SubstringPremium | Medium | String, Binary Search, Dynamic Programming +3 |
| 1081 | Smallest Subsequence of Distinct Characters | Medium | Stack, Greedy, String +1 |
| 1102 | Path With Maximum Minimum ValuePremium | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1124 | Longest Well-Performing Interval | Medium | Stack, Array, Hash Table +2 |
| 1130 | Minimum Cost Tree From Leaf Values | Medium | Stack, Greedy, Array +2 |
| 1182 | Shortest Distance to Target ColorPremium | Medium | Array, Binary Search, Dynamic Programming |
| 1201 | Ugly Number III | Medium | Math, Binary Search, Combinatorics +1 |
| 1283 | Find the Smallest Divisor Given a Threshold | Medium | Array, Binary Search |
| 1292 | Maximum Side Length of a Square with Sum Less than or Equal to Threshold | Medium | Array, Binary Search, Matrix +1 |
| 1300 | Sum of Mutated Array Closest to Target | Medium | Array, Binary Search, Sorting |
| 1428 | Leftmost Column with at Least a OnePremium | Medium | Array, Binary Search, Interactive +1 |
| 1438 | Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit | Medium | Queue, Array, Ordered Set +3 |
| 1482 | Minimum Number of Days to Make m Bouquets | Medium | Array, Binary Search |
| 1504 | Count Submatrices With All Ones | Medium | Stack, Array, Dynamic Programming +2 |
| 1533 | Find the Index of the Large IntegerPremium | Medium | Array, Binary Search, Interactive |
| 1552 | Magnetic Force Between Two Balls | Medium | Array, Binary Search, Sorting |
| 1574 | Shortest Subarray to be Removed to Make Array Sorted | Medium | Stack, Array, Two Pointers +2 |
| 1618 | Maximum Font to Fit a Sentence in a ScreenPremium | Medium | Array, String, Binary Search +1 |
| 1631 | Path With Minimum Effort | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1648 | Sell Diminishing-Valued Colored Balls | Medium | Greedy, Array, Math +3 |
| 1673 | Find the Most Competitive Subsequence | Medium | Stack, Greedy, Array +1 |
| 1696 | Jump Game VI | Medium | Queue, Array, Dynamic Programming +2 |
| 1760 | Minimum Limit of Balls in a Bag | Medium | Array, Binary Search |
| 1762 | Buildings With an Ocean ViewPremium | Medium | Stack, Array, Monotonic Stack |
| 1802 | Maximum Value at a Given Index in a Bounded Array | Medium | Greedy, Math, Binary Search |
| 1818 | Minimum Absolute Sum Difference | Medium | Array, Binary Search, Ordered Set +1 |
| 1856 | Maximum Subarray Min-Product | Medium | Stack, Array, Prefix Sum +1 |
| 1870 | Minimum Speed to Arrive on Time | Medium | Array, Binary Search |
| 1891 | Cutting RibbonsPremium | Medium | Array, Binary Search |
| 1894 | Find the Student that Will Replace the Chalk | Medium | Array, Binary Search, Prefix Sum +1 |
| 1901 | Find a Peak Element II | Medium | Array, Binary Search, Matrix |
| 1950 | Maximum of Minimum Values in All SubarraysPremium | Medium | Stack, Array, Monotonic Stack |
| 1954 | Minimum Garden Perimeter to Collect Enough Apples | Medium | Math, Binary Search |
| 1966 | Binary Searchable Numbers in an Unsorted ArrayPremium | Medium | Array, Binary Search |
| 1996 | The Number of Weak Characters in the Game | Medium | Stack, Greedy, Array +2 |
| 2054 | Two Best Non-Overlapping Events | Medium | Array, Binary Search, Dynamic Programming +2 |
| 2055 | Plates Between Candles | Medium | Array, String, Binary Search +1 |
| 2064 | Minimized Maximum of Products Distributed to Any Store | Medium | Greedy, Array, Binary Search |
| 2070 | Most Beautiful Item for Each Query | Medium | Array, Binary Search, Sorting |
| 2104 | Sum of Subarray Ranges | Medium | Stack, Array, Monotonic Stack |
| 2137 | Pour Water Between Buckets to Make Water Levels EqualPremium | Medium | Array, Binary Search |
| 2187 | Minimum Time to Complete Trips | Medium | Array, Binary Search |
| 2226 | Maximum Candies Allocated to K Children | Medium | Array, Binary Search |
| 2282 | Number of People That Can Be Seen in a GridPremium | Medium | Stack, Array, Matrix +1 |
| 2289 | Steps to Make Array Non-decreasing | Medium | Stack, Array, Linked List +1 |
| 2297 | Jump Game VIIIPremium | Medium | Stack, Graph, Array +3 |
| 2333 | Minimum Sum of Squared Difference | Medium | Greedy, Array, Binary Search +2 |
| 2345 | Finding the Number of Visible MountainsPremium | Medium | Stack, Array, Sorting +1 |
| 2358 | Maximum Number of Groups Entering a Competition | Medium | Greedy, Array, Math +1 |
| 2387 | Median of a Row Wise Sorted MatrixPremium | Medium | Array, Binary Search, Matrix |
| 2439 | Minimize Maximum of Array | Medium | Greedy, Array, Binary Search +2 |
| 2476 | Closest Nodes Queries in a Binary Search Tree | Medium | Tree, Depth-First Search, Binary Search Tree +3 |
| 2487 | Remove Nodes From Linked List | Medium | Stack, Recursion, Linked List +1 |
| 2498 | Frog Jump II | Medium | Greedy, Array, Binary Search |
| 2513 | Minimize the Maximum of Two Arrays | Medium | Math, Binary Search, Number Theory |
| 2517 | Maximum Tastiness of Candy Basket | Medium | Greedy, Array, Binary Search +1 |
| 2557 | Maximum Number of Integers to Choose From a Range IIPremium | Medium | Greedy, Array, Binary Search +1 |
| 2560 | House Robber IV | Medium | Greedy, Array, Binary Search +1 |
| 2594 | Minimum Time to Repair Cars | Medium | Array, Binary Search |
| 2601 | Prime Subtraction Operation | Medium | Greedy, Array, Math +2 |
| 2602 | Minimum Operations to Make All Array Elements Equal | Medium | Array, Binary Search, Prefix Sum +1 |
| 2616 | Minimize the Maximum Difference of Pairs | Medium | Greedy, Array, Binary Search +2 |
| 2762 | Continuous Subarrays | Medium | Queue, Array, Ordered Set +3 |
| 2812 | Find the Safest Path in a Grid | Medium | Breadth-First Search, Union Find, Array +3 |
| 2817 | Minimum Absolute Difference Between Elements With Constraint | Medium | Array, Binary Search, Ordered Set |
| 2826 | Sorting Three Groups | Medium | Array, Binary Search, Dynamic Programming |
| 2832 | Maximal Range That Each Element Is Maximum in ItPremium | Medium | Stack, Array, Monotonic Stack |
| 2861 | Maximum Number of Alloys | Medium | Array, Binary Search |
| 2863 | Maximum Length of Semi-Decreasing SubarraysPremium | Medium | Stack, Array, Sorting +1 |
| 2865 | Beautiful Towers I | Medium | Stack, Array, Monotonic Stack |
| 2866 | Beautiful Towers II | Medium | Stack, Array, Monotonic Stack |
| 2936 | Number of Equal Numbers BlocksPremium | Medium | Array, Binary Search, Interactive |
| 2944 | Minimum Number of Coins for Fruits | Medium | Queue, Array, Dynamic Programming +2 |
| 2967 | Minimum Cost to Make Array Equalindromic | Medium | Greedy, Array, Math +2 |
Hard (89)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 4 | Median of Two Sorted Arrays | Hard | Array, Binary Search, Divide and Conquer |
| 42 | Trapping Rain Water | Hard | Stack, Array, Two Pointers +2 |
| 84 | Largest Rectangle in Histogram | Hard | Stack, Array, Monotonic Stack |
| 85 | Maximal Rectangle | Hard | Stack, Array, Dynamic Programming +2 |
| 154 | Find Minimum in Rotated Sorted Array II | Hard | Array, Binary Search |
| 239 | Sliding Window Maximum | Hard | Queue, Array, Sliding Window +2 |
| 315 | Count of Smaller Numbers After Self | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 321 | Create Maximum Number | Hard | Stack, Greedy, Array +2 |
| 327 | Count of Range Sum | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 354 | Russian Doll Envelopes | Hard | Array, Binary Search, Dynamic Programming +1 |
| 363 | Max Sum of Rectangle No Larger Than K | Hard | Array, Binary Search, Matrix +2 |
| 410 | Split Array Largest Sum | Hard | Greedy, Array, Binary Search +2 |
| 483 | Smallest Good Base | Hard | Math, Binary Search |
| 493 | Reverse Pairs | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 668 | Kth Smallest Number in Multiplication Table | Hard | Math, Binary Search |
| 732 | My Calendar III | Hard | Design, Segment Tree, Binary Search +2 |
| 778 | Swim in Rising Water | Hard | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1235 | Maximum Profit in Job Scheduling | Hard | Array, Binary Search, Dynamic Programming +1 |
| 1851 | Minimum Interval to Include Each Query | Hard | Array, Binary Search, Sorting +2 |
| 302 | Smallest Rectangle Enclosing Black PixelsPremium | Hard | Depth-First Search, Breadth-First Search, Array +2 |
| 644 | Maximum Average Subarray IIPremium | Hard | Array, Binary Search, Prefix Sum |
| 683 | K Empty SlotsPremium | Hard | Binary Indexed Tree, Segment Tree, Queue +5 |
| 768 | Max Chunks To Make Sorted II | Hard | Stack, Greedy, Array +2 |
| 774 | Minimize Max Distance to Gas StationPremium | Hard | Array, Binary Search |
| 793 | Preimage Size of Factorial Zeroes Function | Hard | Math, Binary Search |
| 862 | Shortest Subarray with Sum at Least K | Hard | Queue, Array, Binary Search +4 |
| 878 | Nth Magical Number | Hard | Math, Binary Search |
| 887 | Super Egg Drop | Hard | Math, Binary Search, Dynamic Programming |
| 902 | Numbers At Most N Given Digit Set | Hard | Array, Math, String +2 |
| 975 | Odd Even Jump | Hard | Stack, Array, Dynamic Programming +3 |
| 1063 | Number of Valid SubarraysPremium | Hard | Stack, Array, Monotonic Stack |
| 1095 | Find in Mountain Array | Hard | Array, Binary Search, Interactive |
| 1157 | Online Majority Element In Subarray | Hard | Design, Binary Indexed Tree, Segment Tree +2 |
| 1187 | Make Array Strictly Increasing | Hard | Array, Binary Search, Dynamic Programming +1 |
| 1231 | Divide ChocolatePremium | Hard | Array, Binary Search |
| 1425 | Constrained Subsequence Sum | Hard | Queue, Array, Dynamic Programming +3 |
| 1439 | Find the Kth Smallest Sum of a Matrix With Sorted Rows | Hard | Array, Binary Search, Matrix +1 |
| 1483 | Kth Ancestor of a Tree Node | Hard | Bit Manipulation, Tree, Depth-First Search +4 |
| 1499 | Max Value of Equation | Hard | Queue, Array, Sliding Window +2 |
| 1521 | Find a Value of a Mysterious Function Closest to Target | Hard | Bit Manipulation, Segment Tree, Array +1 |
| 1526 | Minimum Number of Increments on Subarrays to Form a Target Array | Hard | Stack, Greedy, Array +2 |
| 1649 | Create Sorted Array through Instructions | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 1671 | Minimum Number of Removals to Make Mountain Array | Hard | Greedy, Array, Binary Search +1 |
| 1687 | Delivering Boxes from Storage to Ports | Hard | Segment Tree, Queue, Array +4 |
| 1739 | Building Boxes | Hard | Greedy, Math, Binary Search |
| 1751 | Maximum Number of Events That Can Be Attended II | Hard | Array, Binary Search, Dynamic Programming +1 |
| 1776 | Car Fleet II | Hard | Stack, Array, Math +2 |
| 1793 | Maximum Score of a Good Subarray | Hard | Stack, Array, Two Pointers +2 |
| 1847 | Closest Room | Hard | Array, Binary Search, Ordered Set +1 |
| 1862 | Sum of Floored Pairs | Hard | Array, Math, Binary Search +1 |
| 1889 | Minimum Space Wasted From Packaging | Hard | Array, Binary Search, Prefix Sum +1 |
| 1923 | Longest Common Subpath | Hard | Array, Binary Search, Suffix Array +2 |
| 1944 | Number of Visible People in a Queue | Hard | Stack, Array, Monotonic Stack |
| 1956 | Minimum Time For K Virus Variants to SpreadPremium | Hard | Geometry, Array, Math +2 |
| 1964 | Find the Longest Valid Obstacle Course at Each Position | Hard | Binary Indexed Tree, Array, Binary Search |
| 1970 | Last Day Where You Can Still Cross | Hard | Depth-First Search, Breadth-First Search, Union Find +3 |
| 2030 | Smallest K-Length Subsequence With Occurrences of a Letter | Hard | Stack, Greedy, String +1 |
| 2040 | Kth Smallest Product of Two Sorted Arrays | Hard | Array, Binary Search |
| 2071 | Maximum Number of Tasks You Can Assign | Hard | Greedy, Queue, Array +4 |
| 2111 | Minimum Operations to Make the Array K-Increasing | Hard | Array, Binary Search |
| 2141 | Maximum Running Time of N Computers | Hard | Greedy, Array, Binary Search +1 |
| 2179 | Count Good Triplets in an Array | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 2223 | Sum of Scores of Built Strings | Hard | String, Binary Search, String Matching +3 |
| 2258 | Escape the Spreading Fire | Hard | Breadth-First Search, Array, Binary Search +1 |
| 2281 | Sum of Total Strength of Wizards | Hard | Stack, Array, Prefix Sum +1 |
| 2286 | Booking Concert Tickets in Groups | Hard | Design, Binary Indexed Tree, Segment Tree +1 |
| 2334 | Subarray With Elements Greater Than Varying Threshold | Hard | Stack, Union Find, Array +1 |
| 2355 | Maximum Number of Books You Can TakePremium | Hard | Stack, Array, Dynamic Programming +1 |
| 2398 | Maximum Number of Robots Within Budget | Hard | Queue, Array, Binary Search +4 |
| 2407 | Longest Increasing Subsequence II | Hard | Binary Indexed Tree, Segment Tree, Queue +4 |
| 2426 | Number of Pairs Satisfying Inequality | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 2444 | Count Subarrays With Fixed Bounds | Hard | Queue, Array, Sliding Window +1 |
| 2448 | Minimum Cost to Make Array Equal | Hard | Greedy, Array, Binary Search +2 |
| 2454 | Next Greater Element IV | Hard | Stack, Array, Binary Search +3 |
| 2468 | Split Message Based on Limit | Hard | String, Binary Search, Enumeration |
| 2519 | Count the Number of K-Big IndicesPremium | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 2589 | Minimum Time to Complete All Tasks | Hard | Stack, Greedy, Array +2 |
| 2617 | Minimum Number of Visited Cells in a Grid | Hard | Stack, Breadth-First Search, Union Find +5 |
| 2659 | Make Array Empty | Hard | Greedy, Binary Indexed Tree, Segment Tree +4 |
| 2702 | Minimum Operations to Make Numbers Non-positivePremium | Hard | Array, Binary Search |
| 2736 | Maximum Sum Queries | Hard | Stack, Binary Indexed Tree, Segment Tree +4 |
| 2790 | Maximum Number of Groups With Increasing Length | Hard | Greedy, Array, Math +2 |
| 2818 | Apply Operations to Maximize Score | Hard | Stack, Greedy, Array +4 |
| 2819 | Minimum Relative Loss After Buying ChocolatesPremium | Hard | Array, Binary Search, Prefix Sum +1 |
| 2926 | Maximum Balanced Subsequence Sum | Hard | Binary Indexed Tree, Segment Tree, Array +2 |
| 2940 | Find Building Where Alice and Bob Can Meet | Hard | Stack, Binary Indexed Tree, Segment Tree +4 |
| 2941 | Maximum GCD-Sum of a SubarrayPremium | Hard | Array, Math, Binary Search +1 |
| 2945 | Find Maximum Non-decreasing Array Length | Hard | Stack, Queue, Array +4 |
| 2969 | Minimum Number of Coins for Fruits IIPremium | Hard | Queue, Array, Dynamic Programming +2 |
Related patterns
Problems sit in more than one pattern more often than not, and the overlap is where the interesting follow-up questions live.
Monotonic Stack pattern FAQ
What is the monotonic stack pattern?
A monotonic stack is an ordinary stack with one invariant added: its contents are kept increasing or decreasing, and any element that would break the order is popped first.
How many LeetCode problems use the monotonic stack pattern?
This page lists 225 LeetCode problems that the monotonic stack pattern applies to: 21 Easy, 115 Medium and 89 Hard. 184 of them carry a complete Python solution with complexity analysis.
What is the time complexity of the monotonic stack pattern?
O(n) time and O(n) space. Each index is pushed exactly once and popped at most once, so the inner while loop does not multiply the outer loop: the total number of pops over the whole scan is bounded by n even though one step can pop many elements. The stack is the space, and it holds all n indices in the worst case — an input already sorted in the direction the stack maintains never pops anything until the end.
When should I use the monotonic stack pattern in an interview?
The question is about the nearest larger or smaller element in some direction. You are computing spans, widths or distances between a position and the first position that beats it.
Which monotonic stack problem should I start with?
LeetCode 35. Search Insert Position is the lowest-numbered Easy problem on this page, which makes it the usual starting point: the technique is visible without the problem's own complications getting in the way.
What patterns are related to monotonic stack?
Stack, Sliding Window, Two Pointers, Greedy. Problems frequently sit in more than one of these, and the overlap is where the interesting follow-up questions come from.
More ways in: all 22 patterns, the curated study lists, or the full problem list.
Meet the monotonic stack problem you did not practise
Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.