↓Skip to main content

Stack

LeetCode 32: Longest Valid Parentheses
·3 mins
LeetCode Hard String Dynamic-Programming Stack Bracket-Sequences
LeetCode 32 note: a stack of indices whose bottom is the position just before the current valid run; the length is i - stack[-1]
LeetCode 331: Verify Preorder Serialization of a Binary Tree
·2 mins
LeetCode Medium String Stack Tree Binary-Tree
LeetCode 331 note: count open slots: start with 1, every node fills one, a non-null node opens two more; never go negative and end at exactly 0
LeetCode 173: Binary Search Tree Iterator
·2 mins
LeetCode Medium Stack Tree Design Binary-Search-Tree Binary-Tree Iterator
LeetCode 173 note: flattening to an array is simple but uses O(n) space; the follow-up keeps the left spine on a stack for amortized O(1) and O(h) space
Stack and Monotonic Stack
·6 mins
Algorithm Stack Monotonic-Stack Greedy Simulation
Parsing nested structures, simulations that interact with the previous element, monotonic stacks, greedy deletion for the smallest string, and a lazy-deletion Max Stack
LeetCode 2751: Robot Collisions
·3 mins
LeetCode Hard Stack Simulation Sorting
LeetCode 2751 note: sort by position, push right-moving robots, and let each left-moving robot collide until one side is gone
LeetCode 4045: Count Robot Groups
·3 mins
LeetCode Medium Stack Monotonic-Stack Greedy
LeetCode 4045 note: merge robots already close at t = 0, then go right to left like car fleets, where anything faster than the fleet ahead eventually joins it
LeetCode 716: Max Stack
·3 mins
LeetCode Hard Stack Heap Design Lazy-Deletion
LeetCode 716 note: store unique ids on both the stack and the heap, record deletions by id, and discard deleted entries lazily; tracking values cannot tell duplicates apart
LeetCode 1028: Recover a Tree From Preorder Traversal
·3 mins
LeetCode Hard Tree Binary-Tree Dfs Stack String
LeetCode 1028 note: a stack holds the path from the root to the current node, so its length is the depth
LeetCode 1944: Number of Visible People in a Queue
·2 mins
LeetCode Hard Stack Monotonic-Stack
LeetCode 1944 note: a decreasing monotonic stack from right to left; everyone popped is visible, and so is the person left on top
LeetCode 2355: Maximum Number of Books You Can Take
·2 mins
LeetCode Hard Dynamic-Programming Monotonic-Stack Stack Array
LeetCode 2355 note: ending at i, each shelf to the left takes one fewer book until a bottleneck shelf j with too few books; a monotonic stack finds j, and dp[i] = dp[j] + an arithmetic series
LeetCode 316: Remove Duplicate Letters
·2 mins
LeetCode Medium Stack Monotonic-Stack Greedy String
LeetCode 316 note: a monotonic stack with last positions; pop a larger top that appears again later, and skip letters already on the stack
LeetCode 726: Number of Atoms
·3 mins
LeetCode Hard Stack String Hash-Map Sorting
LeetCode 726 note: tokenize into elements, numbers, and parentheses; handle parentheses with a stack, with a default multiplier of 1