Skip to content

Latest commit

 

History

History
222 lines (219 loc) · 51.9 KB

stack.md

File metadata and controls

222 lines (219 loc) · 51.9 KB

Stack Data Structure Problems

LeetCode problems solved using Stack data structures.

Problems

# Problem Difficulty Algorithm Time Space Explanation
20 Valid Parentheses Easy Stack Push / Pop Parsing O(n) O(n) Explanation
28 Find the Index of the First Occurrence in a String Easy Monotonic Stack Filtering O(n) O(n) Explanation
32 Longest Valid Parentheses Hard Stack Push / Pop Parsing O(n) O(n) Explanation
38 Count and Say Medium Stack Push / Pop Parsing O(n) O(n) Explanation
50 Pow(x, n) Medium Stack Push / Pop Parsing O(n) O(n) Explanation
66 Plus One Easy Stack Push / Pop Parsing O(n) O(n) Explanation
71 Simplify Path Medium Stack Push / Pop Parsing O(n) O(n) Explanation
73 Set Matrix Zeroes Medium Stack Push / Pop Parsing O(n) O(n) Explanation
84 Largest Rectangle in Histogram Hard Monotonic Stack Filtering O(n) O(n) Explanation
107 Binary Tree Level Order Traversal II Medium Stack Push / Pop Parsing O(n) O(n) Explanation
116 Populating Next Right Pointers in Each Node Medium Monotonic Stack Filtering O(n) O(n) Explanation
117 Populating Next Right Pointers in Each Node II Medium Monotonic Stack Filtering O(n) O(n) Explanation
144 Binary Tree Preorder Traversal Easy Stack Push / Pop Parsing O(n) O(n) Explanation
145 Binary Tree Postorder Traversal Easy Stack Push / Pop Parsing O(n) O(n) Explanation
150 Evaluate Reverse Polish Notation Medium Stack Push / Pop Parsing O(n) O(n) Explanation
187 Repeated DNA Sequences Medium Stack Push / Pop Parsing O(n) O(n) Explanation
198 House Robber Medium Stack Push / Pop Parsing O(n) O(n) Explanation
204 Count Primes Medium Stack Push / Pop Parsing O(n) O(n) Explanation
224 Basic Calculator Hard Stack Push / Pop Parsing O(n) O(n) Explanation
241 Different Ways to Add Parentheses Medium Monotonic Stack Filtering O(n) O(n) Explanation
316 Remove Duplicate Letters Medium Stack Push / Pop Parsing O(n) O(n) Explanation
329 Longest Increasing Path in a Matrix Hard Stack Push / Pop Parsing O(n) O(n) Explanation
331 Verify Preorder Serialization of a Binary Tree Medium Stack Push / Pop Parsing O(n) O(n) Explanation
385 Mini Parser Medium Stack Push / Pop Parsing O(n) O(n) Explanation
388 Longest Absolute File Path Medium Stack Push / Pop Parsing O(n) O(n) Explanation
402 Remove K Digits Medium Stack Push / Pop Parsing O(n) O(n) Explanation
406 Queue Reconstruction by Height Medium Stack Push / Pop Parsing O(n) O(n) Explanation
429 N-ary Tree Level Order Traversal Medium Stack Push / Pop Parsing O(n) O(n) Explanation
464 Can I Win Medium Monotonic Stack Filtering O(n) O(n) Explanation
492 Construct the Rectangle Easy Stack Push / Pop Parsing O(n) O(n) Explanation
500 Keyboard Row Easy Stack Push / Pop Parsing O(n) O(n) Explanation
515 Find Largest Value in Each Tree Row Medium Stack Push / Pop Parsing O(n) O(n) Explanation
539 Minimum Time Difference Medium Stack Push / Pop Parsing O(n) O(n) Explanation
550 Game Play Analysis IV Medium Stack Push / Pop Parsing O(n) O(n) Explanation
551 Student Attendance Record I Easy Monotonic Stack Filtering O(n) O(n) Explanation
566 Reshape the Matrix Easy Stack Push / Pop Parsing O(n) O(n) Explanation
589 N-ary Tree Preorder Traversal Easy Stack Push / Pop Parsing O(n) O(n) Explanation
590 N-ary Tree Postorder Traversal Easy Stack Push / Pop Parsing O(n) O(n) Explanation
606 Construct String from Binary Tree Medium Stack Push / Pop Parsing O(n) O(n) Explanation
622 Design Circular Queue Medium Stack Push / Pop Parsing O(n) O(n) Explanation
636 Exclusive Time of Functions Medium Stack Push / Pop Parsing O(n) O(n) Explanation
670 Maximum Swap Medium Stack Push / Pop Parsing O(n) O(n) Explanation
696 Count Binary Substrings Easy Stack Push / Pop Parsing O(n) O(n) Explanation
735 Asteroid Collision Medium Stack Push / Pop Parsing O(n) O(n) Explanation
739 Daily Temperatures Medium Monotonic Stack Filtering O(n) O(n) Explanation
746 Min Cost Climbing Stairs Easy Stack Push / Pop Parsing O(n) O(n) Explanation
766 Toeplitz Matrix Easy Stack Push / Pop Parsing O(n) O(n) Explanation
768 Max Chunks To Make Sorted II Hard Stack Push / Pop Parsing O(n) O(n) Explanation
856 Score of Parentheses Medium Monotonic Stack Filtering O(n) O(n) Explanation
861 Score After Flipping Matrix Medium Stack Push / Pop Parsing O(n) O(n) Explanation
867 Transpose Matrix Easy Stack Push / Pop Parsing O(n) O(n) Explanation
896 Monotonic Array Easy Monotonic Stack Filtering O(n) O(n) Explanation
899 Orderly Queue Hard Stack Push / Pop Parsing O(n) O(n) Explanation
901 Online Stock Span Medium Stack Push / Pop Parsing O(n) O(n) Explanation
918 Maximum Sum Circular Subarray Medium Stack Push / Pop Parsing O(n) O(n) Explanation
921 Minimum Add to Make Parentheses Valid Medium Stack Push / Pop Parsing O(n) O(n) Explanation
950 Reveal Cards In Increasing Order Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1003 Check If Word Is Valid After Substitutions Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1016 Binary String With Substrings Representing 1 To N Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1021 Remove Outermost Parentheses Easy Monotonic Stack Filtering O(n) O(n) Explanation
1030 Matrix Cells in Distance Order Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1046 Last Stone Weight Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1047 Remove All Adjacent Duplicates In String Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1051 Height Checker Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1081 Smallest Subsequence of Distinct Characters Medium Monotonic Stack Filtering O(n) O(n) Explanation
1104 Path In Zigzag Labelled Binary Tree Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1106 Parsing A Boolean Expression Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1111 Maximum Nesting Depth of Two Valid Parentheses Strings Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1129 Shortest Path with Alternating Colors Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1163 Last Substring in Lexicographical Order Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1190 Reverse Substrings Between Each Pair of Parentheses Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1209 Remove All Adjacent Duplicates in String II Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1249 Minimum Remove to Make Valid Parentheses Medium Monotonic Stack Filtering O(n) O(n) Explanation
1266 Minimum Time Visiting All Points Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1289 Minimum Falling Path Sum II Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1302 Deepest Leaves Sum Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1314 Matrix Block Sum Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1325 Delete Leaves With a Given Value Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1337 The K Weakest Rows in a Matrix Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1351 Count Negative Numbers in a Sorted Matrix Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1356 Sort Integers by The Number of 1 Bits Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1359 Count All Valid Pickup and Delivery Options Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1439 Find the Kth Smallest Sum of a Matrix With Sorted Rows Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1441 Build an Array With Stack Operations Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1443 Minimum Time to Collect All Apples in a Tree Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1467 Probability of a Two Boxes Having The Same Number of Distinct Balls Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1475 Final Prices With a Special Discount in a Shop Easy Monotonic Stack Filtering O(n) O(n) Explanation
1496 Path Crossing Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1499 Max Value of Equation Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1521 Find a Value of a Mysterious Function Closest to Target Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1524 Number of Sub-arrays With Odd Sum Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1534 Count Good Triplets Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1541 Minimum Insertions to Balance a Parentheses String Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1544 Make The String Great Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1572 Matrix Diagonal Sum Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1582 Special Positions in a Binary Matrix Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1605 Find Valid Matrix Given Row and Column Sums Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1614 Maximum Nesting Depth of the Parentheses Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1617 Count Subtrees With Max Distance Between Cities Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1620 Coordinate With Maximum Network Quality Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1631 Path With Minimum Effort Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1632 Rank Transform of a Matrix Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1688 Count of Matches in Tournament Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1691 Maximum Height by Stacking Cuboids Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1700 Number of Students Unable to Eat Lunch Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1701 Average Waiting Time Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1702 Maximum Binary String After Change Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1725 Number Of Rectangles That Can Form The Largest Square Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1727 Largest Submatrix With Rearrangements Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1738 Find Kth Largest XOR Coordinate Value Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1758 Minimum Changes To Make Alternating Binary String Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1808 Maximize Number of Nice Divisors Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1854 Maximum Population Year Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1878 Get Biggest Three Rhombus Sums in a Grid Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1886 Determine Whether Matrix Can Be Obtained By Rotation Easy Stack Push / Pop Parsing O(n) O(n) Explanation
1894 Find the Student that Will Replace the Chalk Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1916 Count Ways to Build Rooms in an Ant Colony Hard Stack Push / Pop Parsing O(n) O(n) Explanation
1922 Count Good Numbers Medium Stack Push / Pop Parsing O(n) O(n) Explanation
1975 Maximum Matrix Sum Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2017 Grid Game Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2033 Minimum Operations to Make a Uni-Value Grid Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2104 Sum of Subarray Ranges Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2151 Maximum Good People Based on Statements Hard Stack Push / Pop Parsing O(n) O(n) Explanation
2154 Keep Multiplying Found Values by Two Easy Stack Push / Pop Parsing O(n) O(n) Explanation
2222 Number of Ways to Select Buildings Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2236 Root Equals Sum of Children Easy Stack Push / Pop Parsing O(n) O(n) Explanation
2246 Longest Path With Different Adjacent Characters Hard Stack Push / Pop Parsing O(n) O(n) Explanation
2257 Count Unguarded Cells in the Grid Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2293 Min Max Game Easy Stack Push / Pop Parsing O(n) O(n) Explanation
2303 Calculate Amount Paid in Taxes Easy Stack Push / Pop Parsing O(n) O(n) Explanation
2319 Check if Matrix Is X-Matrix Easy Stack Push / Pop Parsing O(n) O(n) Explanation
2322 Minimum Score After Removals on a Tree Hard Stack Push / Pop Parsing O(n) O(n) Explanation
2373 Largest Local Values in a Matrix Easy Stack Push / Pop Parsing O(n) O(n) Explanation
2375 Construct Smallest Number From DI String Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2380 Time Needed to Rearrange a Binary String Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2390 Removing Stars From a String Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2392 Build a Matrix With Conditions Hard Stack Push / Pop Parsing O(n) O(n) Explanation
2434 Using a Robot to Print the Lexicographically Smallest String Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2467 Most Profitable Path in a Tree Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2477 Minimum Fuel Cost to Report to the Capital Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2503 Maximum Number of Points From Grid Queries Hard Stack Push / Pop Parsing O(n) O(n) Explanation
2536 Increment Submatrices by One Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2577 Minimum Time to Visit a Cell In a Grid Hard Stack Push / Pop Parsing O(n) O(n) Explanation
2596 Check Knight Tour Configuration Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2603 Collect Coins in a Tree Hard Stack Push / Pop Parsing O(n) O(n) Explanation
2639 Find the Width of Columns of a Grid Easy Stack Push / Pop Parsing O(n) O(n) Explanation
2673 Make Costs of Paths Equal in a Binary Tree Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2684 Maximum Number of Moves in a Grid Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2685 Count the Number of Complete Components Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2696 Minimum String Length After Removing Substrings Easy Stack Push / Pop Parsing O(n) O(n) Explanation
2708 Maximum Strength of a Group Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2745 Construct the Longest New String Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2836 Maximize Value of Function in a Ball Passing Game Hard Stack Push / Pop Parsing O(n) O(n) Explanation
2862 Maximum Element-Sum of a Complete Subset of Indices Hard Stack Push / Pop Parsing O(n) O(n) Explanation
2866 Beautiful Towers II Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2867 Count Valid Paths in a Tree Hard Stack Push / Pop Parsing O(n) O(n) Explanation
2884 Modify Columns Easy Stack Push / Pop Parsing O(n) O(n) Explanation
2888 Reshape Data: Concatenate Easy Stack Push / Pop Parsing O(n) O(n) Explanation
2897 Apply Operations on Array to Maximize Sum of Squares Hard Stack Push / Pop Parsing O(n) O(n) Explanation
2906 Construct Product Matrix Medium Stack Push / Pop Parsing O(n) O(n) Explanation
2913 Subarrays Distinct Element Sum of Squares I Easy Monotonic Stack Filtering O(n) O(n) Explanation
2946 Matrix Similarity After Cyclic Shifts Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3015 Count the Number of Houses at a Certain Distance I Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3030 Find the Grid of Region Average Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3033 Modify the Matrix Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3038 Maximum Number of Operations With the Same Score I Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3105 Longest Strictly Increasing or Strictly Decreasing Subarray Easy Monotonic Stack Filtering O(n) O(n) Explanation
3123 Find Edges in Shortest Paths Hard Stack Push / Pop Parsing O(n) O(n) Explanation
3127 Make a Square with the Same Color Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3142 Check if Grid Satisfies Conditions Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3234 Count the Number of Substrings With Dominant Ones Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3248 Snake in Matrix Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3250 Find the Count of Monotonic Pairs I Hard Monotonic Stack Filtering O(n) O(n) Explanation
3251 Find the Count of Monotonic Pairs II Hard Monotonic Stack Filtering O(n) O(n) Explanation
3256 Maximum Value Sum by Placing Three Rooks I Hard Stack Push / Pop Parsing O(n) O(n) Explanation
3310 Remove Methods From Project Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3331 Find Subtree Sizes After Changes Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3345 Smallest Divisible Digit Product I Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3370 Smallest Number With All Set Bits Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3393 Count Paths With the Given XOR Value Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3403 Find the Lexicographically Largest String From the Box I Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3417 Zigzag Grid Traversal With Skip Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3419 Minimize the Maximum Edge Weight of Graph Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3425 Longest Special Path Hard Stack Push / Pop Parsing O(n) O(n) Explanation
3426 Manhattan Distances of All Arrangements of Pieces Hard Stack Push / Pop Parsing O(n) O(n) Explanation
3513 Number of Unique XOR Triplets I Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3532 Path Existence Queries in a Graph I Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3546 Equal Sum Grid Partition I Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3551 Minimum Swaps to Sort by Digit Sum Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3558 Number of Ways to Assign Edge Weights I Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3561 Resulting String After Adjacent Removals Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3634 Minimum Removals to Balance Array Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3638 Maximum Balanced Shipments Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3643 Flip Square Submatrix Vertically Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3650 Minimum Cost Path with Edge Reversals Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3665 Twisted Mirror Path Count Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3694 Distinct Points Reachable After Substring Removal Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3700 Number of ZigZag Arrays II Hard Stack Push / Pop Parsing O(n) O(n) Explanation
3727 Maximum Alternating Sum of Squares Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3731 Find Missing Elements Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3737 Count Subarrays With Majority Element I Medium Monotonic Stack Filtering O(n) O(n) Explanation
3746 Minimum String Length After Balanced Removals Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3765 Complete Prime Number Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3833 Count Dominant Indices Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3858 Minimum Bitwise OR From Grid Medium Stack Push / Pop Parsing O(n) O(n) Explanation
3870 Count Commas in Range Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3898 Find the Degree of Each Vertex Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3963 Create Grid With Exactly One Path Easy Stack Push / Pop Parsing O(n) O(n) Explanation
3988 Create Grid With Exactly K Paths I Medium Stack Push / Pop Parsing O(n) O(n) Explanation
4002 Count Valid Sequences Medium Stack Push / Pop Parsing O(n) O(n) Explanation
4030 Check ASCII Palindromic Easy Stack Push / Pop Parsing O(n) O(n) Explanation
4044 Count Good Cyclic Rotations Medium Stack Push / Pop Parsing O(n) O(n) Explanation
4054 Count Shadow Pairs I Medium Stack Push / Pop Parsing O(n) O(n) Explanation
4062 Transform Array Using Pair Operations Medium Stack Push / Pop Parsing O(n) O(n) Explanation