Monotonic Stack LeetCode Problems: All 58, With Python Solutions
Every problem in this library that LeetCode tags Monotonic Stack — 58 in total, 47 of them with a complete Python solution, a worked example and the time and space complexity of the approach.
- 58 problems
- 2 Easy
- 35 Medium
- 21 Hard
How Monotonic Stack problems are solved
A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Monotonic Stack problems — each one explains the approach, gives a Python template and states its complexity.
- Monotonic Stack — Answer "what is the next greater element" for every position in one pass.
Monotonic Stack problems by difficulty
Problems with a complete Python solution are listed first, then by ascending problem number.
Easy (2)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 496 | Next Greater Element I | Easy | Stack, Array, Hash Table +1 |
| 1475 | Final Prices With a Special Discount in a Shop | Easy | Stack, Array, Monotonic Stack |
Medium (35)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 316 | Remove Duplicate Letters | Medium | Stack, Greedy, String +1 |
| 402 | Remove K Digits | Medium | Stack, Greedy, String +1 |
| 456 | 132 Pattern | Medium | Stack, Array, Binary Search +2 |
| 503 | Next Greater Element II | Medium | Stack, Array, Monotonic Stack |
| 581 | Shortest Unsorted Continuous Subarray | Medium | Stack, Greedy, Array +3 |
| 654 | Maximum Binary Tree | Medium | Stack, Tree, Array +3 |
| 739 | Daily Temperatures | Medium | Stack, Array, Monotonic Stack |
| 769 | Max Chunks To Make Sorted | Medium | Stack, Greedy, Array +2 |
| 853 | Car Fleet | Medium | Stack, Array, Sorting +1 |
| 901 | Online Stock Span | Medium | Stack, Design, Data Stream +1 |
| 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 |
| 1019 | Next Greater Node In Linked List | Medium | Stack, Array, Linked List +1 |
| 1081 | Smallest Subsequence of Distinct Characters | Medium | Stack, Greedy, String +1 |
| 1124 | Longest Well-Performing Interval | Medium | Stack, Array, Hash Table +2 |
| 1130 | Minimum Cost Tree From Leaf Values | Medium | Stack, Greedy, Array +2 |
| 1504 | Count Submatrices With All Ones | Medium | Stack, Array, Dynamic Programming +2 |
| 1574 | Shortest Subarray to be Removed to Make Array Sorted | Medium | Stack, Array, Two Pointers +2 |
| 1673 | Find the Most Competitive Subsequence | Medium | Stack, Greedy, Array +1 |
| 1856 | Maximum Subarray Min-Product | Medium | Stack, Array, Prefix Sum +1 |
| 1996 | The Number of Weak Characters in the Game | Medium | Stack, Greedy, Array +2 |
| 2104 | Sum of Subarray Ranges | Medium | Stack, Array, Monotonic Stack |
| 2289 | Steps to Make Array Non-decreasing | Medium | Stack, Array, Linked List +1 |
| 2487 | Remove Nodes From Linked List | Medium | Stack, Recursion, Linked List +1 |
| 2865 | Beautiful Towers I | Medium | Stack, Array, Monotonic Stack |
| 2866 | Beautiful Towers II | Medium | Stack, Array, Monotonic Stack |
| 255 | Verify Preorder Sequence in Binary Search TreePremium | Medium | Stack, Tree, Binary Search Tree +4 |
| 1762 | Buildings With an Ocean ViewPremium | Medium | Stack, Array, Monotonic Stack |
| 1950 | Maximum of Minimum Values in All SubarraysPremium | Medium | Stack, Array, Monotonic Stack |
| 2282 | Number of People That Can Be Seen in a GridPremium | Medium | Stack, Array, Matrix +1 |
| 2297 | Jump Game VIIIPremium | Medium | Stack, Graph, Array +3 |
| 2345 | Finding the Number of Visible MountainsPremium | Medium | Stack, Array, Sorting +1 |
| 2832 | Maximal Range That Each Element Is Maximum in ItPremium | Medium | Stack, Array, Monotonic Stack |
| 2863 | Maximum Length of Semi-Decreasing SubarraysPremium | Medium | Stack, Array, Sorting +1 |
Hard (21)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 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 |
| 321 | Create Maximum Number | Hard | Stack, Greedy, Array +2 |
| 768 | Max Chunks To Make Sorted II | Hard | Stack, Greedy, Array +2 |
| 975 | Odd Even Jump | Hard | Stack, Array, Dynamic Programming +3 |
| 1526 | Minimum Number of Increments on Subarrays to Form a Target Array | Hard | Stack, Greedy, Array +2 |
| 1776 | Car Fleet II | Hard | Stack, Array, Math +2 |
| 1793 | Maximum Score of a Good Subarray | Hard | Stack, Array, Two Pointers +2 |
| 1944 | Number of Visible People in a Queue | Hard | Stack, Array, Monotonic Stack |
| 2281 | Sum of Total Strength of Wizards | Hard | Stack, Array, Prefix Sum +1 |
| 2334 | Subarray With Elements Greater Than Varying Threshold | Hard | Stack, Union Find, Array +1 |
| 2454 | Next Greater Element IV | Hard | Stack, Array, Binary Search +3 |
| 2617 | Minimum Number of Visited Cells in a Grid | Hard | Stack, Breadth-First Search, Union Find +5 |
| 2736 | Maximum Sum Queries | Hard | Stack, Binary Indexed Tree, Segment Tree +4 |
| 2818 | Apply Operations to Maximize Score | Hard | Stack, Greedy, Array +4 |
| 2940 | Find Building Where Alice and Bob Can Meet | Hard | Stack, Binary Indexed Tree, Segment Tree +4 |
| 2945 | Find Maximum Non-decreasing Array Length | Hard | Stack, Queue, Array +4 |
| 1063 | Number of Valid SubarraysPremium | Hard | Stack, Array, Monotonic Stack |
| 2030 | Smallest K-Length Subsequence With Occurrences of a Letter | Hard | Stack, Greedy, String +1 |
| 2355 | Maximum Number of Books You Can TakePremium | Hard | Stack, Array, Dynamic Programming +1 |
Keep exploring
- Array1,569
- String672
- Hash Table588
- Math485
- Dynamic Programming481
- Sorting392
- Greedy346
- Depth-First Search289
- Binary Search253
- Database249
- Tree225
- Breadth-First Search223
- Matrix216
- Two Pointers201
- Bit Manipulation194
- Binary Tree174
- Heap (Priority Queue)163
- Prefix Sum157
- Stack157
- Simulation144
- Graph138
- Counting126
- Design122
- Sliding Window116
When the Monotonic Stack problem arrives live
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.