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)

#ProblemDifficultyTopics
496Next Greater Element IEasyStack, Array, Hash Table +1
1475Final Prices With a Special Discount in a ShopEasyStack, Array, Monotonic Stack

Medium (35)

#ProblemDifficultyTopics
316Remove Duplicate LettersMediumStack, Greedy, String +1
402Remove K DigitsMediumStack, Greedy, String +1
456132 PatternMediumStack, Array, Binary Search +2
503Next Greater Element IIMediumStack, Array, Monotonic Stack
581Shortest Unsorted Continuous SubarrayMediumStack, Greedy, Array +3
654Maximum Binary TreeMediumStack, Tree, Array +3
739Daily TemperaturesMediumStack, Array, Monotonic Stack
769Max Chunks To Make SortedMediumStack, Greedy, Array +2
853Car FleetMediumStack, Array, Sorting +1
901Online Stock SpanMediumStack, Design, Data Stream +1
907Sum of Subarray MinimumsMediumStack, Array, Dynamic Programming +1
962Maximum Width RampMediumStack, Array, Two Pointers +1
1008Construct Binary Search Tree from Preorder TraversalMediumStack, Tree, Binary Search Tree +3
1019Next Greater Node In Linked ListMediumStack, Array, Linked List +1
1081Smallest Subsequence of Distinct CharactersMediumStack, Greedy, String +1
1124Longest Well-Performing IntervalMediumStack, Array, Hash Table +2
1130Minimum Cost Tree From Leaf ValuesMediumStack, Greedy, Array +2
1504Count Submatrices With All OnesMediumStack, Array, Dynamic Programming +2
1574Shortest Subarray to be Removed to Make Array SortedMediumStack, Array, Two Pointers +2
1673Find the Most Competitive SubsequenceMediumStack, Greedy, Array +1
1856Maximum Subarray Min-ProductMediumStack, Array, Prefix Sum +1
1996The Number of Weak Characters in the GameMediumStack, Greedy, Array +2
2104Sum of Subarray RangesMediumStack, Array, Monotonic Stack
2289Steps to Make Array Non-decreasingMediumStack, Array, Linked List +1
2487Remove Nodes From Linked ListMediumStack, Recursion, Linked List +1
2865Beautiful Towers IMediumStack, Array, Monotonic Stack
2866Beautiful Towers IIMediumStack, Array, Monotonic Stack
255Verify Preorder Sequence in Binary Search TreePremiumMediumStack, Tree, Binary Search Tree +4
1762Buildings With an Ocean ViewPremiumMediumStack, Array, Monotonic Stack
1950Maximum of Minimum Values in All SubarraysPremiumMediumStack, Array, Monotonic Stack
2282Number of People That Can Be Seen in a GridPremiumMediumStack, Array, Matrix +1
2297Jump Game VIIIPremiumMediumStack, Graph, Array +3
2345Finding the Number of Visible MountainsPremiumMediumStack, Array, Sorting +1
2832Maximal Range That Each Element Is Maximum in ItPremiumMediumStack, Array, Monotonic Stack
2863Maximum Length of Semi-Decreasing SubarraysPremiumMediumStack, Array, Sorting +1

Hard (21)

#ProblemDifficultyTopics
42Trapping Rain WaterHardStack, Array, Two Pointers +2
84Largest Rectangle in HistogramHardStack, Array, Monotonic Stack
85Maximal RectangleHardStack, Array, Dynamic Programming +2
321Create Maximum NumberHardStack, Greedy, Array +2
768Max Chunks To Make Sorted IIHardStack, Greedy, Array +2
975Odd Even JumpHardStack, Array, Dynamic Programming +3
1526Minimum Number of Increments on Subarrays to Form a Target ArrayHardStack, Greedy, Array +2
1776Car Fleet IIHardStack, Array, Math +2
1793Maximum Score of a Good SubarrayHardStack, Array, Two Pointers +2
1944Number of Visible People in a QueueHardStack, Array, Monotonic Stack
2281Sum of Total Strength of WizardsHardStack, Array, Prefix Sum +1
2334Subarray With Elements Greater Than Varying ThresholdHardStack, Union Find, Array +1
2454Next Greater Element IVHardStack, Array, Binary Search +3
2617Minimum Number of Visited Cells in a GridHardStack, Breadth-First Search, Union Find +5
2736Maximum Sum QueriesHardStack, Binary Indexed Tree, Segment Tree +4
2818Apply Operations to Maximize ScoreHardStack, Greedy, Array +4
2940Find Building Where Alice and Bob Can MeetHardStack, Binary Indexed Tree, Segment Tree +4
2945Find Maximum Non-decreasing Array LengthHardStack, Queue, Array +4
1063Number of Valid SubarraysPremiumHardStack, Array, Monotonic Stack
2030Smallest K-Length Subsequence With Occurrences of a LetterHardStack, Greedy, String +1
2355Maximum Number of Books You Can TakePremiumHardStack, Array, Dynamic Programming +1

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.