Largest Rectangle Under a Skyline with Forbidden Zones
Stacks & QueuesAuto-graded · 17 test casesLast updated
Tests the ability to apply a monotonic stack to find the largest rectangular area under a histogram while skipping columns that are marked as forbidden.
Largest Rectangle Under a Skyline with Forbidden Zones
You are given the elevation profile of a city skyline as a list of non-negative integer heights, one per column. Each column has unit width. Additionally, some columns are forbidden: a rectangle may not include any forbidden column, even partially.
Your task is to find the area of the largest axis-aligned rectangle that:
- Fits entirely under the skyline (its height at every included column does not exceed that column's height), and
- Spans only a contiguous block of columns, none of which are forbidden.
A rectangle spanning columns l through r (inclusive) has area (r - l + 1) * min(heights[l..r]).
Return the maximum such area. If no valid rectangle exists (e.g., all columns are forbidden or all heights are zero), return 0.
Constraints
1 <= len(heights) == len(forbidden) <= 20000 <= heights[i] <= 10^6forbidden[i]is0(allowed) or1(forbidden)- A rectangle must span at least one non-forbidden column with height
> 0to have positive area. - The answer fits in a standard Python
int.
Examples
Example 1
Input: largest_skyline_rectangle([3, 1, 3, 3, 2], [0, 0, 0, 0, 0])
Output: 6
Example 2
Input: largest_skyline_rectangle([4, 2, 0, 3, 5, 1], [0, 0, 1, 0, 0, 0])
Output: 6
Example 3
Input: largest_skyline_rectangle([5, 5, 5, 5], [1, 1, 1, 1])
Output: 0
Hints
Hint 1
Split the column array into maximal contiguous runs of non-forbidden columns, then solve each run independently.
Hint 2
Within each non-forbidden run, the classic largest-rectangle-in-histogram algorithm using a monotonic (non-decreasing) stack runs in O(n) time.
Hint 3
When you pop a bar from the stack to compute its candidate area, the width extends from the element now on top of the stack (exclusive) to the current index (exclusive), capped by the run boundaries.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
The forbidden columns act as hard barriers: no rectangle may cross them. This naturally partitions the column array into maximal contiguous runs of allowed columns. Within each run the problem reduces to the classic Largest Rectangle in Histogram, which is solved optimally with a monotonic stack in O(m) time per run of length m.
Algorithm
- Scan for runs. Iterate through the array, skip forbidden columns, and identify each contiguous block
[i, j)of allowed columns. - Monotonic stack per run. For each run, maintain a stack of column indices whose heights are in non-decreasing order. When a shorter bar is encountered (or the run ends), pop taller bars and compute the area each would form as the limiting height of a rectangle:
- The rectangle's height is the popped bar's height.
- Its left boundary is just after the new stack top (or the run's start if the stack is empty).
- Its right boundary is just before the current position.
- Track global maximum across all runs.
Correctness
- Splitting on forbidden columns is exact: any rectangle that included a forbidden column is illegal regardless of area.
- The classic monotonic stack argument guarantees we consider every possible (l, r, min_height) triple within a run exactly once: each bar is pushed and popped at most once.
- The sentinel height-0 bar appended after each run flushes all remaining stack entries, ensuring no candidate is missed.
Complexity
- Time: O(n) — each column is pushed and popped at most once across all runs.
- Space: O(n) — for the stack, which holds at most all columns of one run.
Reference Solution
from typing import List
def largest_skyline_rectangle(heights: list, forbidden: list) -> int:
n = len(heights)
best = 0
i = 0
while i < n:
if forbidden[i] == 1:
i += 1
continue
# Find the end of this non-forbidden run [i, j)
j = i
while j < n and forbidden[j] == 0:
j += 1
# Largest rectangle in histogram on heights[i:j]
stack = [] # indices in non-decreasing height order
for k in range(i, j + 1):
h = heights[k] if k < j else 0 # sentinel 0 to flush stack
while stack and heights[stack[-1]] > h:
height = heights[stack.pop()]
left = stack[-1] + 1 if stack else i
width = k - left
best = max(best, height * width)
stack.append(k)
i = j
return best