Stack LeetCode Problems: All 157, With Python Solutions

Every problem in this library that LeetCode tags Stack 157 in total, 126 of them with a complete Python solution, a worked example and the time and space complexity of the approach.

  • 157 problems
  • 22 Easy
  • 89 Medium
  • 46 Hard

How Stack problems are solved

A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Stack problems — each one explains the approach, gives a Python template and states its complexity.

  • Stack — When the most recent unresolved thing is the one that matters, use a stack.

Stack problems by difficulty

Problems with a complete Python solution are listed first, then by ascending problem number.

Easy (22)

#ProblemDifficultyTopics
20Valid ParenthesesEasyStack, String
94Binary Tree Inorder TraversalEasyStack, Tree, Depth-First Search +1
144Binary Tree Preorder TraversalEasyStack, Tree, Depth-First Search +1
145Binary Tree Postorder TraversalEasyStack, Tree, Depth-First Search +1
225Implement Stack using QueuesEasyStack, Design, Queue
232Implement Queue using StacksEasyStack, Design, Queue
234Palindrome Linked ListEasyStack, Recursion, Linked List +1
496Next Greater Element IEasyStack, Array, Hash Table +1
589N-ary Tree Preorder TraversalEasyStack, Tree, Depth-First Search
590N-ary Tree Postorder TraversalEasyStack, Tree, Depth-First Search
682Baseball GameEasyStack, Array, Simulation
844Backspace String CompareEasyStack, Two Pointers, String +1
897Increasing Order Search TreeEasyStack, Tree, Depth-First Search +2
1021Remove Outermost ParenthesesEasyStack, String
1047Remove All Adjacent Duplicates In StringEasyStack, String
1475Final Prices With a Special Discount in a ShopEasyStack, Array, Monotonic Stack
1544Make The String GreatEasyStack, String
1598Crawler Log FolderEasyStack, Array, String
1614Maximum Nesting Depth of the ParenthesesEasyStack, String
1700Number of Students Unable to Eat LunchEasyStack, Queue, Array +1
2000Reverse Prefix of WordEasyStack, Two Pointers, String
2696Minimum String Length After Removing SubstringsEasyStack, String, Simulation

Medium (89)

#ProblemDifficultyTopics
71Simplify PathMediumStack, String
114Flatten Binary Tree to Linked ListMediumStack, Tree, Depth-First Search +2
143Reorder ListMediumStack, Recursion, Linked List +1
150Evaluate Reverse Polish NotationMediumStack, Array, Math
155Min StackMediumStack, Design
173Binary Search Tree IteratorMediumStack, Tree, Design +3
227Basic Calculator IIMediumStack, Math, String
316Remove Duplicate LettersMediumStack, Greedy, String +1
331Verify Preorder Serialization of a Binary TreeMediumStack, Tree, String +1
341Flatten Nested List IteratorMediumStack, Tree, Depth-First Search +3
385Mini ParserMediumStack, Depth-First Search, String
388Longest Absolute File PathMediumStack, Depth-First Search, String
394Decode StringMediumStack, Recursion, String
402Remove K DigitsMediumStack, Greedy, String +1
445Add Two Numbers IIMediumStack, Linked List, Math
456132 PatternMediumStack, Array, Binary Search +2
503Next Greater Element IIMediumStack, Array, Monotonic Stack
581Shortest Unsorted Continuous SubarrayMediumStack, Greedy, Array +3
636Exclusive Time of FunctionsMediumStack, Array
654Maximum Binary TreeMediumStack, Tree, Array +3
678Valid Parenthesis StringMediumStack, Greedy, String +1
735Asteroid CollisionMediumStack, Array, Simulation
739Daily TemperaturesMediumStack, Array, Monotonic Stack
769Max Chunks To Make SortedMediumStack, Greedy, Array +2
853Car FleetMediumStack, Array, Sorting +1
856Score of ParenthesesMediumStack, String
880Decoded String at IndexMediumStack, String
901Online Stock SpanMediumStack, Design, Data Stream +1
907Sum of Subarray MinimumsMediumStack, Array, Dynamic Programming +1
921Minimum Add to Make Parentheses ValidMediumStack, Greedy, String
946Validate Stack SequencesMediumStack, Array, Simulation
962Maximum Width RampMediumStack, Array, Two Pointers +1
1003Check If Word Is Valid After SubstitutionsMediumStack, String
1006Clumsy FactorialMediumStack, Math, Simulation
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
1111Maximum Nesting Depth of Two Valid Parentheses StringsMediumStack, String
1124Longest Well-Performing IntervalMediumStack, Array, Hash Table +2
1130Minimum Cost Tree From Leaf ValuesMediumStack, Greedy, Array +2
1190Reverse Substrings Between Each Pair of ParenthesesMediumStack, String
1209Remove All Adjacent Duplicates in String IIMediumStack, String
1249Minimum Remove to Make Valid ParenthesesMediumStack, String
1381Design a Stack With Increment OperationMediumStack, Design, Array
1441Build an Array With Stack OperationsMediumStack, Array, Simulation
1472Design Browser HistoryMediumStack, Design, Array +3
1504Count Submatrices With All OnesMediumStack, Array, Dynamic Programming +2
1541Minimum Insertions to Balance a Parentheses StringMediumStack, Greedy, String
1574Shortest Subarray to be Removed to Make Array SortedMediumStack, Array, Two Pointers +2
1653Minimum Deletions to Make String BalancedMediumStack, String, Dynamic Programming
1673Find the Most Competitive SubsequenceMediumStack, Greedy, Array +1
1717Maximum Score From Removing SubstringsMediumStack, Greedy, String
1856Maximum Subarray Min-ProductMediumStack, Array, Prefix Sum +1
1910Remove All Occurrences of a SubstringMediumStack, String, Simulation
1963Minimum Number of Swaps to Make the String BalancedMediumStack, Greedy, Two Pointers +1
1996The Number of Weak Characters in the GameMediumStack, Greedy, Array +2
2104Sum of Subarray RangesMediumStack, Array, Monotonic Stack
2116Check if a Parentheses String Can Be ValidMediumStack, Greedy, String
2130Maximum Twin Sum of a Linked ListMediumStack, Linked List, Two Pointers
2211Count Collisions on a RoadMediumStack, String, Simulation
2216Minimum Deletions to Make Array BeautifulMediumStack, Greedy, Array
2289Steps to Make Array Non-decreasingMediumStack, Array, Linked List +1
2375Construct Smallest Number From DI StringMediumStack, Greedy, String +1
2390Removing Stars From a StringMediumStack, String, Simulation
2434Using a Robot to Print the Lexicographically Smallest StringMediumStack, Greedy, Hash Table +1
2487Remove Nodes From Linked ListMediumStack, Recursion, Linked List +1
2645Minimum Additions to Make Valid StringMediumStack, Greedy, String +1
2816Double a Number Represented as a Linked ListMediumStack, Linked List, Math
2865Beautiful Towers IMediumStack, Array, Monotonic Stack
2866Beautiful Towers IIMediumStack, Array, Monotonic Stack
255Verify Preorder Sequence in Binary Search TreePremiumMediumStack, Tree, Binary Search Tree +4
364Nested List Weight Sum IIPremiumMediumStack, Depth-First Search, Breadth-First Search
426Convert Binary Search Tree to Sorted Doubly Linked ListPremiumMediumStack, Tree, Depth-First Search +4
439Ternary Expression ParserPremiumMediumStack, Recursion, String
484Find PermutationPremiumMediumStack, Greedy, Array +1
536Construct Binary Tree from StringPremiumMediumStack, Tree, Depth-First Search +2
1087Brace ExpansionPremiumMediumStack, Breadth-First Search, String +2
1214Two Sum BSTsPremiumMediumStack, Tree, Depth-First Search +4
1265Print Immutable Linked List in ReversePremiumMediumStack, Recursion, Linked List +1
1586Binary Search Tree Iterator IIPremiumMediumStack, Tree, Design +3
1628Design an Expression Tree With Evaluate FunctionPremiumMediumStack, Tree, Design +3
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
2764Is Array a Preorder of Some ‌Binary TreePremiumMediumStack, Tree, Depth-First Search +1
2832Maximal Range That Each Element Is Maximum in ItPremiumMediumStack, Array, Monotonic Stack
2863Maximum Length of Semi-Decreasing SubarraysPremiumMediumStack, Array, Sorting +1

Hard (46)

#ProblemDifficultyTopics
32Longest Valid ParenthesesHardStack, String, Dynamic Programming
42Trapping Rain WaterHardStack, Array, Two Pointers +2
84Largest Rectangle in HistogramHardStack, Array, Monotonic Stack
85Maximal RectangleHardStack, Array, Dynamic Programming +2
224Basic CalculatorHardStack, Recursion, Math +1
321Create Maximum NumberHardStack, Greedy, Array +2
488Zuma GameHardStack, Breadth-First Search, Memoization +2
591Tag ValidatorHardStack, String
736Parse Lisp ExpressionHardStack, Recursion, Hash Table +1
768Max Chunks To Make Sorted IIHardStack, Greedy, Array +2
895Maximum Frequency StackHardStack, Design, Hash Table +1
936Stamping The SequenceHardStack, Greedy, Queue +1
975Odd Even JumpHardStack, Array, Dynamic Programming +3
1096Brace Expansion IIHardStack, Breadth-First Search, Hash Table +3
1106Parsing A Boolean ExpressionHardStack, Recursion, String
1172Dinner Plate StacksHardStack, Design, Hash Table +1
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
2019The Score of Students Solving Math ExpressionHardStack, Memoization, Array +4
2197Replace Non-Coprime Numbers in ArrayHardStack, Array, Math +1
2281Sum of Total Strength of WizardsHardStack, Array, Prefix Sum +1
2296Design a Text EditorHardStack, Design, Linked List +3
2334Subarray With Elements Greater Than Varying ThresholdHardStack, Union Find, Array +1
2454Next Greater Element IVHardStack, Array, Binary Search +3
2589Minimum Time to Complete All TasksHardStack, Greedy, Array +2
2617Minimum Number of Visited Cells in a GridHardStack, Breadth-First Search, Union Find +5
2736Maximum Sum QueriesHardStack, Binary Indexed Tree, Segment Tree +4
2751Robot CollisionsHardStack, Array, Sorting +1
2813Maximum Elegance of a K-Length SubsequenceHardStack, Greedy, Array +3
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
272Closest Binary Search Tree Value IIPremiumHardStack, Tree, Depth-First Search +4
716Max StackPremiumHardStack, Design, Linked List +2
726Number of AtomsHardStack, Hash Table, String +1
770Basic Calculator IVHardStack, Recursion, Hash Table +2
772Basic Calculator IIIPremiumHardStack, Recursion, Math +1
1063Number of Valid SubarraysPremiumHardStack, Array, Monotonic Stack
1597Build Binary Expression Tree From Infix ExpressionPremiumHardStack, Tree, String +1
1896Minimum Cost to Change the Final Value of ExpressionHardStack, Math, String +1
2030Smallest K-Length Subsequence With Occurrences of a LetterHardStack, Greedy, String +1
2254Design Video Sharing PlatformPremiumHardStack, Design, Hash Table +1
2355Maximum Number of Books You Can TakePremiumHardStack, Array, Dynamic Programming +1
2524Maximum Frequency Score of a SubarrayPremiumHardStack, Array, Hash Table +2

When the 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.