InferenceAI

Enter at least two characters to search.

Start free

Largest Histogram Rectangle

hardStack40 min50 XP

Bars of width 1 have heights heights. Return the area of the largest rectangle that fits entirely under the histogram.

Requirements

  • Run in O(n)O(n) time.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[2,1,5,6,2,3]]
TASKlargest_histogram_rectangle()
EXPECTED OUTPUT10

Start with the input values shown in this example.

Example 1 of 3

Input: heights = [2,1,5,6,2,3]

Output: 10

Explanation: classic

Input: heights = [2,4]

Output: 4

Explanation: two bars

Input: heights = []

Output: 0

Explanation: empty

Hint 1

For each bar, find how far it can extend left and right while staying at least as tall.

Hint 2

A stack of increasing heights gives both boundaries in one pass.

Topics

DSAStackPython