This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 2: Structures
Keep a stack in sorted order; whatever you pop just found its answer.
Builds on Stacks.
The question to ask
For each element, do I need the nearest element to one side that's bigger or smaller than it?
For "next greater element" questions the brute force checks every later element for each position, O(n²). A stack of indices kept in decreasing value order fixes it: when a bigger value arrives, everything smaller on the stack has just met its next greater element, so pop them and record the answer. Each index is pushed and popped once, so it's O(n). Flip the comparison for next smaller; the same idea finds the span of each bar in histogram questions.
Time O(n) · Space O(n)
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
Keep bar indices with increasing heights. A shorter bar pops taller ones, and each popped bar's rectangle runs from just after the new top of the stack to just before the current index. A sentinel 0 at the end flushes the stack.
function largestRectangleArea(heights: number[]): number {
const stack: number[] = [];
let best = 0;
for (let i = 0; i <= heights.length; i++) {
const height = i === heights.length ? 0 : heights[i];
while (stack.length > 0 && height < heights[stack[stack.length - 1]]) {
const top = stack.pop() as number;
const left = stack.length > 0 ? stack[stack.length - 1] + 1 : 0;
best = Math.max(best, heights[top] * (i - left));
}
stack.push(i);
}
return best;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
For each value of nums1, find the next greater element to its right in nums2, or -1.
"Next greater" is the name of the pattern. Precompute it for all of nums2 with one stack, store in a map.
Not attempted yet
Given daily temperatures, return for each day how many days you have to wait for a warmer temperature, or 0.
"How many days" until a "warmer temperature" is next greater, measured as an index distance.
Not attempted yet
Design a class that receives daily prices and returns the span: how many consecutive days up to today had a price less than or equal to today's.
"Span" back to the previous greater price: keep [price, span] pairs and pop smaller ones, adding their spans.
Not attempted yet
Cars drive toward a target at fixed speeds and can't pass; a car that catches up joins that car fleet. Return how many fleets arrive.
Sort by position, compute arrival times from the front. A car that would arrive sooner than the fleet ahead merges into it: a stack of fleet times.
Not attempted yet
Remove k digits from a number string to make the smallest possible number.
To make the number small, a digit bigger than the one after it must go. An increasing stack, popping while removals remain.
Not attempted yet
Return the sum of min(subarray) over every contiguous subarray, modulo 1e9 + 7.
"Sum of subarray minimums": each element is the minimum for (distance to previous smaller) × (distance to next smaller) subarrays.
Not attempted yet
Given bar heights of a histogram with width 1 each, return the area of the largest rectangle.
"Histogram" plus "largest rectangle": each bar's rectangle spans to the nearest shorter bar on each side.
Not attempted yet
Given a binary matrix, find the largest rectangle containing only 1s and return its area.
Treat each row as the floor of a histogram of consecutive 1s above it, then run largest rectangle in histogram per row.
Not attempted yet