Largest Block Under a Skyline of Columns
Stacks & QueuesAuto-graded · 23 test casesLast updated
Compute the largest rectangle that fits under a row of unit-width columns, testing monotonic stack usage and boundary reasoning over contiguous ranges.
You are given a list columns of n non-negative integers describing a skyline. Each entry is the height of one column, and every column is exactly 1 unit wide. Columns sit side by side with no gaps, in the order given.
You may pick any non-empty run of adjacent columns and draw an axis-aligned rectangle that rests on the baseline and stays fully inside the chosen columns. For a run from index i to index j (inclusive), the tallest rectangle that fits has:
- height equal to the smallest value among
columns[i..j], and - width
j - i + 1.
Its area is height multiplied by width.
Return the largest area achievable over all possible runs.
Notes:
- A run of a single column is allowed.
- If every column has height
0, the answer is0.
Constraints
1 <= n <= 1000000 <= columns[i] <= 1000000000- The answer fits in a 64-bit signed integer.
Examples
Example 1
Input: largest_skyline_rectangle([2, 1, 5, 6, 2, 3])
Output: 10
Example 2
Input: largest_skyline_rectangle([2, 4])
Output: 4
Hints
Hint 1
Any rectangle is limited by its shortest column. Instead of enumerating all runs, ask: for each column, how far can a rectangle extend left and right while that column stays the minimum?
Hint 2
Checking every pair (i, j) is O(n^2). To find the nearest strictly shorter column on each side in one pass, keep a stack of indices whose heights never decrease from bottom to top.
Hint 3
When a new column is shorter than the one on top of the stack, the top column cannot extend further right, so pop it and compute its area. Its left edge is one past the new stack top (or 0 if empty). Append a height-0 sentinel to flush the remaining indices.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Every candidate rectangle is limited by its shortest column. So for each column k, consider the widest rectangle in which columns[k] is the minimum. It stretches left and right until it meets a strictly shorter column. If the nearest shorter column on the left is at L and on the right at R, the area is columns[k] * (R - L - 1). The answer is the maximum of this over all k.
Trying every pair of endpoints is O(n^2), too slow for n = 100000. A monotonic stack finds both boundaries in a single pass:
- Keep a stack of indices whose heights are non-decreasing from bottom to top.
- Scan left to right. When the current height is smaller than the height at the top of the stack, that top column can no longer extend right, so its right boundary is the current index
i. - Pop it. The new stack top (if any) is the nearest column to the left that is not taller, so the rectangle starts at
top + 1; if the stack is empty it starts at0. - The area for the popped column is its height times
i - left. - Append a sentinel height
0at the end of the scan so every remaining index gets popped without a separate cleanup loop.
Why it is correct
- A column is popped at the first index to its right with a strictly smaller height. Everything between was pushed while it was on the stack, so all those columns are at least as tall, which makes the right boundary exact.
- The element beneath a popped index in the stack is not taller than it, and every index between them was popped earlier by something no shorter than the popped column, so all columns in that gap are at least as tall. The left boundary is therefore valid.
- With equal heights, a column may compute a width that is too short because its left neighbour in the stack has the same height. The leftmost column of that equal-height run is popped at the same right boundary but with the true left boundary, so it yields the full width. The optimal rectangle is thus always evaluated by at least one column.
Complexity
- Time: O(n), since each index is pushed once and popped at most once.
- Space: O(n) for the stack and the sentinel-extended copy.
Reference solution
from typing import List
def largest_skyline_rectangle(columns: List[int]) -> int:
stack = [] # indices with non-decreasing heights
best = 0
for i, height in enumerate(columns + [0]): # sentinel 0 flushes the stack
while stack and columns[stack[-1]] > height:
h = columns[stack.pop()]
left = stack[-1] + 1 if stack else 0
best = max(best, h * (i - left))
stack.append(i)
return best