Home/Data Structures & Algorithms/Stacks, Queues & Ring Buffers

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Monotonic stacks, lock-free ring buffers, arithmetic parsers, and low-latency message queues.
Daily Temperatures: How to use an Index-Tracking Monotonic Stack for next warmer day in O(N)
This problem is the cleanest introductory template for the Monotonic Decreasing Stack pattern. Let's look at why storing indices unlocks the distance calculation. 1. The Mental Model Imagine people waiting in line holding temperature tickets. If temperatures are dropping: [73, 71, 69], nobody has foRead more
This problem is the cleanest introductory template for the Monotonic Decreasing Stack pattern. Let’s look at why storing indices unlocks the distance calculation.
1. The Mental Model
Imagine people waiting in line holding temperature tickets. If temperatures are dropping:
[73, 71, 69], nobody has found a warmer day yet! So everyone has to stay waiting in line.Now, a warm day arrives:
72!69sees72 > 69. Their wait is over! They step out of line.71sees72 > 71. Their wait is over! They step out of line.73sees72 < 73.72is not warm enough for them! The person with73stays waiting in line, and the day with72joins the line behind them.2. Why Store Indices Instead of Values?
If you only push temperature numbers (e.g.
69) onto the stack, when a warmer day72pops69, you know that a warmer day happened, but you don’t know how many days elapsed!By pushing the array index
prev_dayonto the stack:You calculate the exact time difference in $O(1)$ and write directly to the output array!
Clean Python 3.12 Implementation
Complexity Breakdown
- Time Complexity:
- Space Complexity:
See lessO(N). Every index is pushed onto the stack once and popped at most once. Total operations: $le 2N$.O(N)for the stack in the worst-case of strictly decreasing temperatures (e.g.[100, 90, 80, 70]).How does a Monotonic Stack solve Largest Rectangle in Histogram in a single pass?
The Largest Rectangle in Histogram is famous because it feels like magic until you see the visual geometry behind it. Let's demystify it once and for all. 1. The Core Realization For any bar at index k with height H = heights[k], what is the widest rectangle you can make using H as the height? The rRead more
The Largest Rectangle in Histogram is famous because it feels like magic until you see the visual geometry behind it. Let’s demystify it once and for all.
1. The Core Realization
For any bar at index
kwith heightH = heights[k], what is the widest rectangle you can make usingHas the height?The rectangle can extend as far left as possible until it hits a bar shorter than
H, and as far right as possible until it hits another bar shorter thanH.So the entire problem boils down to finding two things for every bar:
2. Why a Monotonic Increasing Stack?
A monotonic stack keeps indices of bars whose heights are strictly increasing:
[2, 4, 6, 8].As long as the next bar is taller or equal, the rectangle could potentially keep growing, so we just push its index onto the stack.
The Trigger: The moment you encounter a bar that is shorter than the top of the stack (say we see a bar of height
3when the stack top is8), you have found the Right Boundary for that8! The bar of height8cannot extend any further to the right. Its journey is finished.When you pop
8:iis its first shorter bar on the right.Therefore, the width of the rectangle bounded by height
His simply:width = (i - stack[-1] - 1).3. Clean Python 3.12 Implementation with Sentinel Trick
4. Step-by-Step Trace with Numbers
Let’s trace
heights = [2, 1, 5, 6, 2, 3]with sentinel[2, 1, 5, 6, 2, 3, 0]:i = 0 (h=2): Stack =[0]i = 1 (h=1): 1 < 2! Pop0(h=2). Stack empty → width = 1. Area =2 * 1 = 2. Push 1. Stack =[1].i = 2 (h=5): 5 > 1. Push 2. Stack =[1, 2].i = 3 (h=6): 6 > 5. Push 3. Stack =[1, 2, 3].i = 4 (h=2): 2 < 6!3(h=6): right = 4, left = 2 → width =4 - 2 - 1 = 1. Area =6 * 1 = 6.2(h=5): right = 4, left = 1 → width =4 - 1 - 1 = 2. Area =5 * 2 = 10!Push 4. Stack =
[1, 4].0sentinel cleanly flushes all remaining elements.Max Area = 10 (from bars of height 5 and 6).
5. Why is this strictly O(N)?
Even though there is a
See lesswhileloop inside theforloop, every index is pushed onto the stack exactly once and popped from the stack at most once. Total operations across the entire array are at most2N. That is a rock-solid, linearO(N)runtime withO(N)memory.