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