Stacks & Queues — Complete Pattern Guide
Problems 251–300 · 50 posts · Easy × 10, Medium × 30, Hard × 10
Core Patterns
| # | Pattern | Key Idea | Problems |
|---|
| 1 | Monotonic Stack (decreasing) | Pop when current > top | Next Greater Element, Daily Temperatures |
| 2 | Monotonic Stack (increasing) | Pop when current < top | Largest Rectangle in Histogram |
| 3 | BFS / Level-Order | Queue + while queue | Binary Tree Level Order, Rotting Oranges |
| 4 | Two-Stack Tricks | Simulate queue / min-stack | Min Stack, Queue via Stacks |
| 5 | Bracket Matching | Push open, pop on close | Valid Parentheses, Decode String |
| 6 | Expression Evaluation | Operator + operand stacks | Basic Calculator, Polish Notation |
| 7 | Deque / Sliding Window Max | Monotonic deque, pop front+back | Sliding Window Maximum |
Templates
Monotonic Decreasing Stack (Next Greater)
stack = []
res = [0] * n
for i in range(n):
while stack and nums[stack[-1]] < nums[i]:
res[stack.pop()] = nums[i]
stack.append(i)
Monotonic Increasing Stack (Previous Smaller)
stack = []
for i in range(n):
while stack and nums[stack[-1]] >= nums[i]:
stack.pop()
left[i] = stack[-1] if stack else -1
stack.append(i)
BFS Template
from collections import deque
queue = deque([start])
visited = {start}
while queue:
node = queue.popleft()
for neighbor in get_neighbors(node):
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
Min Stack
class MinStack:
def __init__(self): self.stack = []; self.min_stack = []
def push(self, val):
self.stack.append(val)
self.min_stack.append(min(val, self.min_stack[-1] if self.min_stack else val))
def pop(self): self.stack.pop(); self.min_stack.pop()
def top(self): return self.stack[-1]
def getMin(self): return self.min_stack[-1]
Sliding Window Maximum (Deque)
from collections import deque
dq = deque()
res = []
for i in range(n):
while dq and dq[0] < i - k + 1: dq.popleft()
while dq and nums[dq[-1]] < nums[i]: dq.pop()
dq.append(i)
if i >= k - 1: res.append(nums[dq[0]])
Problem Index — Stacks & Queues
Easy (251–260)
| # | Problem | Pattern |
|---|
| 251 | Valid Parentheses | Bracket Matching |
| 252 | Implement Stack using Queues | Design |
| 253 | Implement Queue using Stacks | Two-Stack |
| 254 | Min Stack | Two-Stack / Min Tracking |
| 255 | Baseball Game | Stack Simulation |
| 256 | Backspace String Compare | Stack / Two Pointer |
| 257 | Crawler Log Folder | Stack |
| 258 | Number of Recent Calls | Queue |
| 259 | Make the String Great | Stack |
| 260 | Next Warmer Temperature (Easy) | Monotonic Stack |
Medium (261–290)
| # | Problem | Pattern |
|---|
| 261 | Daily Temperatures | Monotonic Stack |
| 262 | Next Greater Element I | Monotonic Stack |
| 263 | Next Greater Element II (Circular) | Monotonic Stack |
| 264 | Online Stock Span | Monotonic Stack |
| 265 | Remove K Digits | Monotonic Stack |
| 266 | 132 Pattern | Monotonic Stack |
| 267 | Monotonic Stack Remove Duplicates to Get Smallest | Monotonic Stack |
| 268 | Decode String | Stack + Counter |
| 269 | Evaluate Reverse Polish Notation | Stack |
| 270 | Flatten Nested List Iterator | Stack Design |
| 271 | Design Circular Queue | Array Design |
| 272 | Design Circular Deque | Deque Design |
| 273 | Sliding Window Maximum | Monotonic Deque |
| 274 | Jump Game VI | Monotonic Deque + DP |
| 275 | Shortest Subarray with Sum at Least K | Deque + Prefix Sum |
| 276 | Max Queue | Deque Design |
| 277 | Task Scheduler | Greedy / Queue |
| 278 | Rearrange String k Distance Apart | Greedy + Heap |
| 279 | Number of Visible People in Queue | Monotonic Stack |
| 280 | Asteroid Collision | Stack |
| 281 | Score of Parentheses | Stack |
| 282 | Sum of Subarray Minimums | Monotonic Stack |
| 283 | Basic Calculator II | Stack + Precedence |
| 284 | Rotting Oranges | Multi-Source BFS |
| 285 | Number of Islands | BFS / DFS |
| 286 | Walls and Gates | Multi-Source BFS |
| 287 | Pacific Atlantic Water Flow | BFS |
| 288 | Course Schedule | Topological Sort BFS |
| 289 | Course Schedule II | Topological Sort |
| 290 | Word Ladder | BFS |
Hard (291–300)
| # | Problem | Pattern |
|---|
| 291 | Largest Rectangle in Histogram | Monotonic Stack |
| 292 | Maximal Rectangle | Mono Stack on Matrix |
| 293 | Basic Calculator I | Stack + Recursion |
| 294 | Max Frequency Stack | Freq Buckets |
| 295 | Design Hit Counter | Queue / Binary Search |
| 296 | Sliding Window Maximum (Hard variant) | Deque |
| 297 | Trapping Rain Water (Stack approach) | Monotonic Stack |
| 298 | Find Median from Data Stream | Two Heaps |
| 299 | Serialize and Deserialize Binary Tree | BFS Queue |
| 300 | Stacks & Queues Master Recap | Cheatsheet |