Stack LeetCode Problems: All 157,
With Python Solutions

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

Easy
22
Medium
89
Hard
46
1 pattern

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.

Eyes only

When the Stack problem arrives live.

In the live round, Stealth Interview reads the 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 says. It's a desktop app for macOS and Windows, invisible to screen sharing.

Stack problems by difficulty

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

Easy (22)

Easy problems
No.Problem
0020Valid Parentheses
0094Binary Tree Inorder Traversal
0144Binary Tree Preorder Traversal
0145Binary Tree Postorder Traversal
0225Implement Stack using Queues
0232Implement Queue using Stacks
0234Palindrome Linked List
0496Next Greater Element I
0589N-ary Tree Preorder Traversal
0590N-ary Tree Postorder Traversal
0682Baseball Game
0844Backspace String Compare
0897Increasing Order Search Tree
1021Remove Outermost Parentheses
1047Remove All Adjacent Duplicates In String
1475Final Prices With a Special Discount in a Shop
1544Make The String Great
1598Crawler Log Folder
1614Maximum Nesting Depth of the Parentheses
1700Number of Students Unable to Eat Lunch
2000Reverse Prefix of Word
2696Minimum String Length After Removing Substrings

Medium (89)

Medium problems
No.Problem
0071Simplify Path
0114Flatten Binary Tree to Linked List
0143Reorder List
0150Evaluate Reverse Polish Notation
0155Min Stack
0173Binary Search Tree Iterator
0227Basic Calculator II
0316Remove Duplicate Letters
0331Verify Preorder Serialization of a Binary Tree
0341Flatten Nested List Iterator
0385Mini Parser
0388Longest Absolute File Path
0394Decode String
0402Remove K Digits
0445Add Two Numbers II
0456132 Pattern
0503Next Greater Element II
0581Shortest Unsorted Continuous Subarray
0636Exclusive Time of Functions
0654Maximum Binary Tree
0678Valid Parenthesis String
0735Asteroid Collision
0739Daily Temperatures
0769Max Chunks To Make Sorted
0853Car Fleet
0856Score of Parentheses
0880Decoded String at Index
0901Online Stock Span
0907Sum of Subarray Minimums
0921Minimum Add to Make Parentheses Valid
0946Validate Stack Sequences
0962Maximum Width Ramp
1003Check If Word Is Valid After Substitutions
1006Clumsy Factorial
1008Construct Binary Search Tree from Preorder Traversal
1019Next Greater Node In Linked List
1081Smallest Subsequence of Distinct Characters
1111Maximum Nesting Depth of Two Valid Parentheses Strings
1124Longest Well-Performing Interval
1130Minimum Cost Tree From Leaf Values
1190Reverse Substrings Between Each Pair of Parentheses
1209Remove All Adjacent Duplicates in String II
1249Minimum Remove to Make Valid Parentheses
1381Design a Stack With Increment Operation
1441Build an Array With Stack Operations
1472Design Browser History
1504Count Submatrices With All Ones
1541Minimum Insertions to Balance a Parentheses String
1574Shortest Subarray to be Removed to Make Array Sorted
1653Minimum Deletions to Make String Balanced
1673Find the Most Competitive Subsequence
1717Maximum Score From Removing Substrings
1856Maximum Subarray Min-Product
1910Remove All Occurrences of a Substring
1963Minimum Number of Swaps to Make the String Balanced
1996The Number of Weak Characters in the Game
2104Sum of Subarray Ranges
2116Check if a Parentheses String Can Be Valid
2130Maximum Twin Sum of a Linked List
2211Count Collisions on a Road
2216Minimum Deletions to Make Array Beautiful
2289Steps to Make Array Non-decreasing
2375Construct Smallest Number From DI String
2390Removing Stars From a String
2434Using a Robot to Print the Lexicographically Smallest String
2487Remove Nodes From Linked List
2645Minimum Additions to Make Valid String
2816Double a Number Represented as a Linked List
2865Beautiful Towers I
2866Beautiful Towers II
0255Verify Preorder Sequence in Binary Search TreePremium
0364Nested List Weight Sum IIPremium
0426Convert Binary Search Tree to Sorted Doubly Linked ListPremium
0439Ternary Expression ParserPremium
0484Find PermutationPremium
0536Construct Binary Tree from StringPremium
1087Brace ExpansionPremium
1214Two Sum BSTsPremium
1265Print Immutable Linked List in ReversePremium
1586Binary Search Tree Iterator IIPremium
1628Design an Expression Tree With Evaluate FunctionPremium
1762Buildings With an Ocean ViewPremium
1950Maximum of Minimum Values in All SubarraysPremium
2282Number of People That Can Be Seen in a GridPremium
2297Jump Game VIIIPremium
2345Finding the Number of Visible MountainsPremium
2764Is Array a Preorder of Some ‌Binary TreePremium
2832Maximal Range That Each Element Is Maximum in ItPremium
2863Maximum Length of Semi-Decreasing SubarraysPremium

Hard (46)

Hard problems
No.Problem
0032Longest Valid Parentheses
0042Trapping Rain Water
0084Largest Rectangle in Histogram
0085Maximal Rectangle
0224Basic Calculator
0321Create Maximum Number
0488Zuma Game
0591Tag Validator
0736Parse Lisp Expression
0768Max Chunks To Make Sorted II
0895Maximum Frequency Stack
0936Stamping The Sequence
0975Odd Even Jump
1096Brace Expansion II
1106Parsing A Boolean Expression
1172Dinner Plate Stacks
1526Minimum Number of Increments on Subarrays to Form a Target Array
1776Car Fleet II
1793Maximum Score of a Good Subarray
1944Number of Visible People in a Queue
2019The Score of Students Solving Math Expression
2197Replace Non-Coprime Numbers in Array
2281Sum of Total Strength of Wizards
2296Design a Text Editor
2334Subarray With Elements Greater Than Varying Threshold
2454Next Greater Element IV
2589Minimum Time to Complete All Tasks
2617Minimum Number of Visited Cells in a Grid
2736Maximum Sum Queries
2751Robot Collisions
2813Maximum Elegance of a K-Length Subsequence
2818Apply Operations to Maximize Score
2940Find Building Where Alice and Bob Can Meet
2945Find Maximum Non-decreasing Array Length
0272Closest Binary Search Tree Value IIPremium
0716Max StackPremium
0726Number of Atoms
0770Basic Calculator IV
0772Basic Calculator IIIPremium
1063Number of Valid SubarraysPremium
1597Build Binary Expression Tree From Infix ExpressionPremium
1896Minimum Cost to Change the Final Value of Expression
2030Smallest K-Length Subsequence With Occurrences of a Letter
2254Design Video Sharing PlatformPremium
2355Maximum Number of Books You Can TakePremium
2524Maximum Frequency Score of a SubarrayPremium