Skip to main content

Command Palette

Search for a command to run...

stack problem

Published
2 min readView as Markdown

Problem

LinkCategory / PatternKey idea / TemplateDifficultyNotes
Next Greater Element (Right)https://leetcode.com/problems/next-greater-element-i/Monotonic stack — next greaterMaintain decreasing stack; pop while current > stack.top. O(n).Easy
Next Greater Element (Left)https://leetcode.com/problems/next-greater-element-ii/ (adapt left)Monotonic stack — mirrorIterate left→right; pop until top > current.Easy
Next Smaller Element (Right)https://leetcode.com/problems/next-greater-element-iii/ (adapt smaller)Monotonic stack (increasing)Pop while current < stack.top. O(n).Easy
Next Smaller Element (Left)(Custom implementation)Monotonic stackMirror of NSE right.Easy
Next Greater Element II (Circular)https://leetcode.com/problems/next-greater-element-ii/Monotonic stack — circular arrayIterate twice; use modulo indexing. O(n).Medium
Stock Spanhttps://leetcode.com/problems/online-stock-span/Stack (indexes of decreasing prices)Pop until price[top] > current; span = i - stack.top. O(n).Easy
Daily Temperatureshttps://leetcode.com/problems/daily-temperatures/Monotonic stackNGE logic for next warmer day.Medium
Largest Rectangle in Histogramhttps://leetcode.com/problems/largest-rectangle-in-histogram/Monotonic stack — NSL + NSRUse NSL/NSR to compute max area. O(n).Hard
Sum of Subarray Minimumshttps://leetcode.com/problems/sum-of-subarray-minimums/Monotonic stack + combinatoricsContribution = value * left_span * right_span. O(n).Hard
Sum of Subarray Rangeshttps://leetcode.com/problems/sum-of-subarray-ranges/Two monotonic passes (max-min)Sum(max) - Sum(min) using stacks. O(n).Hard
Remove K Digitshttps://leetcode.com/problems/remove-k-digits/Stack/greedyKeep increasing stack; remove when top > current; strip leading zeros.Medium
Asteroid Collisionhttps://leetcode.com/problems/asteroid-collision/Stack simulationHandle sign collisions; compare magnitudes.Medium
Remove Outermost Parentheseshttps://leetcode.com/problems/remove-outermost-parentheses/Stack / depth counterTrack depth; skip outermost.Easy
Minimum Remove to Make Valid Parentheseshttps://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/Stack + mark invalidTrack '(' indices; mark unmatched; rebuild.Medium
Valid Parenthesis Stringhttps://leetcode.com/problems/valid-parenthesis-string/Greedy / two-passTrack possible open count range.Medium
Score of Parentheseshttps://leetcode.com/problems/score-of-parentheses/Stack or depth countingScore rule: ()=1, AB=A+B, (A)=2*A.Easy
Longest Valid Parentheseshttps://leetcode.com/problems/longest-valid-parentheses/Stack or DPTrack base index; extend valid lengths.Hard
Minimum Swaps for Bracket Balancinghttps://www.geeksforgeeks.org/problems/minimum-swaps-for-bracket-balancing2704/1Greedy / pointerTrack imbalance and count swaps.Medium

More from this blog

Amit singh's blog

235 posts