HomeStack
Stack
Last-in first-out — perfect for matching and 'nearest' problems.
38 shown
- Valid ParenthesesOpeners wait on a stack for their matchanimatedEasy
- Min StackO(1) minimum alongside push/popanimatedMedium
- Evaluate Reverse Polish NotationOperators pop their two operandsanimatedMedium
- Daily TemperaturesMonotonic stack of 'waiting' daysanimatedMedium
- Practice problems
- Implement Stack using ArraysAn array plus a top indexEasy
- Implement Queue using ArraysA circular buffer with two indicesEasy
- Implement Stack using QueueRotate after each pushEasy
- Implement Queue using StackTwo stacks, in and outEasy
- Implement stack using LinkedlistPush and pop at the headEasy
- Implement queue using LinkedlistHead to dequeue, tail to enqueueEasy
- Balanced ParanthesisPush openers, match on closeanimatedEasy
- Implement Min StackStore the minimum alongside each elementanimatedHard
- Infix to Postfix ConversionOperands out, operators on the stackMedium
- Prefix to Infix ConversionScan right to left, combine two operandsMedium
- Prefix to Postfix ConversionRight to left, operator lastMedium
- Postfix to Prefix ConversionLeft to right, operator firstMedium
- Postfix to Infix ConversionLeft to right, bracket each joinEasy
- Infix to Prefix ConversionReverse, convert, reverse againMedium
- Next Greater ElementDecreasing stack, popped means answeredanimatedMedium
- Next Greater Element - 2Walk the array twice for the wrap-aroundanimatedMedium
- Next Smaller ElementIncreasing stack insteadanimatedMedium
- Number of Greater Elements to the RightCount, so a stack is not enoughanimatedEasy
- Trapping RainwaterWater above a bar is bounded by the taller wallsanimatedHard
- Sum of Subarray MinimumsCount the subarrays each element dominatesanimatedMedium
- Asteroid CollisionThe stack holds the survivors so farMedium
- Sum of Subarray RangesSum of maxima minus sum of minimaanimatedMedium
- Remove K DigitsGreedily drop a digit bigger than its successorMedium
- Largest rectangle in a histogramEach bar's rectangle spans to the smaller neighboursanimatedHard
- Maximum RectanglesA histogram per rowHard
- Sliding Window MaximumA deque of useful indicesanimatedHard
- Stock span problemJump back to the previous greater priceanimatedHard
- Celebrity ProblemEach question eliminates one personHard
- LRU CacheHash map plus a doubly linked listMedium
- LFU CacheOne list per frequency, plus a minimumHard
- Decode StringStack the count and the prefix at each bracketMedium
- Longest Valid ParenthesesStack the indices, measure from the last unmatchedMedium
- Monotonic StackKeep the stack sorted; each element enters and leaves onceMedium
- Largest Rectangle in HistogramMonotonic stack over the bar heightsanimatedMedium