Mural on the Stepped Wall
A school courtyard's old garden wall is built from columns of bricks, each one brick wide, side by side with no gaps. Some columns have lost bricks, so the top is a jagged staircase; a column of height 0 has none left.
The art class wants the biggest rectangular mural possible. It must sit on the ground, cover whole adjacent columns, and stay on bricks: no taller than the shortest column it spans.
Your task
Write largestMural(heights). heights[i] is the number of bricks in column i, each brick 1 × 1. Return the largest area, in bricks, of a rectangle spanning one or more consecutive columns and no taller than the shortest of them.
Return 0 for an empty wall or a wall with no bricks.
largestMural(heights: int[]) → int
Examples
heights = [3, 5, 4, 1, 6, 2]
9
Columns 0 to 2 (heights 3, 5, 4) are all at least 3 tall, so they give 3 × 3 = 9. The tallest column alone gives 6, columns 1 and 2 give 4 × 2 = 8, and any rectangle that includes column 3 is at most 1 tall, so it gives at most 1 × 6 = 6.
heights = [2, 4, 4, 0, 5]
8
Columns 1 and 2 give 4 × 2 = 8. Column 3 has no bricks, so no rectangle can cross it, and the column of height 5 alone gives only 5.
Constraints
- 0 ≤ heights.length ≤ 100,000
- 0 ≤ heights[i] ≤ 10,000
Start with brute force
Try every pair of start and end columns, keep a running minimum height as the end moves right, and compute minimum × width.
That is O(n²): about 5 billion pairs for 100,000 columns, rechecking the same runs from every start. Each column only needs to know where it is blocked on each side.
Hints
0 of 4 shown- Hint 1
Fix one column, say the 4 in
[3, 5, 4, 1, 6, 2], and make the mural exactly that tall. How far left and right can it stretch? - Hint 2
Every best mural is exactly as tall as its shortest column. So for each column, take the widest rectangle of its height: it stretches until a shorter column on each side. The answer is the best of these n candidates.
- Hint 3
So each column needs its nearest shorter column on the left and on the right. That "next smaller" question is answered in one pass by a stack of columns with increasing heights.
- Hint 4
Walk left to right with a stack of indexes whose heights increase. When column
iis shorter than the top, pop the top:iis its right limit, and the new top (or -1 if empty) its left limit, so its area isheight × (i - left - 1). Pushi. At the end, add one extra column of height 0 to pop everything left.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Try each column as "the shortest": its mural spreads sideways until a shorter column on each side. A stack of increasing heights finds both limits. When a shorter column arrives and pops a column, the newcomer is the popped column's right limit, and the column below it on the stack is its left limit.
Trace [3, 5, 4, 1, 6, 2] (stack shown as index(height)):
- At i = 0 and i = 1 the heights 3 and 5 are increasing, so both are pushed. The stack is 0(3), 1(5).
- At i = 2 (height 4), the top has height 5, which is at least 4, so index 1 is popped with left 0, right 2, width 1 and area 5 × 1 = 5. Then 2 is pushed, and the stack is 0(3), 2(4).
- At i = 3 (height 1), index 2 is popped with left 0, width 3 - 0 - 1 = 2 and area 4 × 2 = 8. Index 0 is popped next; the stack is then empty, so left = -1, width 3 and area 3 × 3 = 9. Then 3 is pushed, and the stack is 3(1).
- At i = 4 (height 6), the height is larger than the top, so 4 is pushed. The stack is 3(1), 4(6).
- At i = 5 (height 2), index 4 is popped with left 3, width 1 and area 6. Then 5 is pushed, and the stack is 3(1), 5(2).
- At i = 6, the extra column of height 0, index 5 is popped with left 3, width 2 and area 4, and then index 3 is popped with left -1, width 6 and area 6.
The best area is 9. Two details matter. The final height-0 step flushes columns that never met a shorter one. And a popped column's area uses the index below it on the stack, not its own, because every column between them was taller and already popped.
function largestMural(heights) {
const stack = []; // indexes; heights increase from bottom to top
let best = 0;
for (let i = 0; i <= heights.length; i++) {
const h = i < heights.length ? heights[i] : 0; // a final 0 flushes the stack
while (stack.length > 0 && heights[stack[stack.length - 1]] >= h) {
const top = stack.pop();
// shorter neighbours: left is the new top, right is i
const left = stack.length > 0 ? stack[stack.length - 1] : -1;
best = Math.max(best, heights[top] * (i - left - 1));
}
stack.push(i);
}
return best;
}def largest_mural(heights):
stack = [] # indexes; heights increase from bottom to top
best = 0
for i in range(len(heights) + 1):
h = heights[i] if i < len(heights) else 0 # a final 0 flushes the stack
while stack and heights[stack[-1]] >= h:
top = stack.pop()
# shorter neighbours: left is the new top, right is i
left = stack[-1] if stack else -1
best = max(best, heights[top] * (i - left - 1))
stack.append(i)
return bestRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.