Monotonic-stack

27 articles

dsa6 min read

Daily Temperatures — Monotonic Stack Next Greater Element [LC 739]

LeetCode 739 — asked at Amazon, Google, and Meta. Find the number of days until a warmer temperature using a monotonic decreasing stack. Store indices not temperatures, pop when current day is warmer, and solve six related problems with the same O(n) pattern.

Read →
dsa6 min read

Largest Rectangle in Histogram — Monotonic Stack O(n) [LC 84]

LeetCode 84 — asked at Amazon, Google, and Microsoft. Find the largest rectangle in a histogram using a monotonic increasing stack. For each bar, find the nearest shorter bars on both sides to compute the maximum width. O(n) time, O(n) space.

Read →
dsa6 min read

Next Greater Element I — Your First Monotonic Stack Problem

LC 496 Next Greater Element I is the canonical entry point for monotonic stack thinking. Master the decreasing stack pattern and hash map lookup here, and you will recognize the same structure in Daily Temperatures, Largest Rectangle in Histogram, and Trapping Rain Water.

Read →
dsa6 min read

Daily Temperatures — Monotonic Stack With Index Distances

LC 739 Daily Temperatures is the most important medium-level monotonic stack problem. Unlike Next Greater Element, the stack stores indices so you can compute waiting distances. Every FAANG company uses this to gauge stack fluency.

Read →
dsa6 min read

Remove K Digits — Build the Smallest Number with a Monotonic Stack

LC 402 Remove K Digits uses a monotonic increasing stack to greedily eliminate digits that make the number larger. The three-part answer construction — pop k times, trim trailing removals, strip leading zeros — is the exact pattern that trips candidates in interviews.

Read →
dsa6 min read

132 Pattern — Scanning Right-to-Left with a Monotonic Stack

LC 456 132 Pattern requires finding i < j < k where nums[i] < nums[k] < nums[j]. The right-to-left decreasing stack maintains the "2" candidate — the key insight that makes an O(n) solution possible where left-to-right fails.

Read →
dsa6 min read

Maximal Rectangle — Applying Histogram Analysis Row by Row

LC 85 Maximal Rectangle is a hard problem that reduces to running Largest Rectangle in Histogram on each row. Build cumulative height histograms row by row and apply the O(n) monotonic stack solution — the reduction is the key insight.

Read →
dsa6 min read

Next Greater Element II — Circular Array Monotonic Stack

LC 503 Next Greater Element II extends the NGE pattern to circular arrays. Use a monotonic decreasing stack with a double-pass (iterate 2n) and modular indexing to handle wrap-around — a critical adaptation tested as a follow-up at every FAANG company.

Read →
dsa7 min read

Daily Temperatures — Monotonic Decreasing Stack Explained

Find how many days until a warmer temperature using a monotonic decreasing stack of unresolved day indices. The canonical monotonic stack problem asked at every FAANG company — master the pattern here and unlock 20+ harder problems.

Read →
dsa7 min read

Next Greater Element I — Monotonic Stack with HashMap

Find the next greater element for each query using a monotonic stack on nums2 and a hash map lookup in O(n+m) time. A classic application of the monotonic stack pattern combined with hash map lookups for efficient query answering.

Read →
dsa8 min read

Next Greater Element II — Circular Array Monotonic Stack

Find the next greater element in a circular array by processing 2n indices with modulo indexing and a monotonic stack. The circular variant of the classic monotonic stack pattern — an elegant trick that extends NGE I to handle wrap-around in O(n) time.

Read →
dsa7 min read

Online Stock Span — Monotonic Stack with Span Accumulation

Calculate the stock price span using a monotonic decreasing stack that stores (price, span) pairs and accumulates spans in O(1) amortized time. A classic streaming design problem that tests span accumulation and the monotonic stack pattern.

Read →
dsa8 min read

Remove K Digits — Monotonic Increasing Stack Greedy

Remove k digits from a number string to form the smallest possible number using a monotonic increasing stack with greedy removal. A key FAANG problem testing greedy thinking, stack manipulation, and edge case handling with leading zeros.

Read →