Quick Overview

Find the maximum rectangle area under adjacent unit-width histogram bars. Handle repeated heights and zero bars, explain the stack invariant, and use arithmetic wide enough for large areas.

Find the largest rectangle beneath a histogram

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given nonnegative heights of adjacent histogram bars, each of width one, return the largest area of an axis-aligned rectangle that fits entirely beneath the histogram. A rectangle spans a contiguous sequence of bars and its height cannot exceed any bar in that sequence. ### Function Signature ```python def largest_histogram_rectangle(heights: list[int]) -> int: ``` ### Rules - Empty input has area zero. - A zero-height bar prevents a positive-height rectangle from crossing it. - Return only the maximum area, so tied rectangles require no positional tie-break. ### Constraints These are exercise bounds for the referenced histogram task. - `0 <= len(heights) <= 100000`. - `0 <= heights[i] <= 1000000000`. - The answer can exceed signed 32-bit range. Use 64-bit arithmetic. The maximum is `100000000000000`, below `2^53`. ### Examples ```text Input: heights = [2, 1, 5, 6, 2, 3] Output: 10 ``` The adjacent bars of heights five and six support a rectangle of height five and width two. ```text Input: heights = [4, 4, 0, 3] Output: 8 ``` The first two bars support area eight. The zero-height bar separates them from the final bar.

Overview: Find the maximum rectangle area under adjacent unit-width histogram bars. Handle repeated heights and zero bars, explain the stack invariant, and use arithmetic wide enough for large areas.

Given nonnegative heights of adjacent histogram bars, each of width one, return the largest area of an axis-aligned rectangle that fits entirely beneath the histogram. A rectangle spans a contiguous sequence of bars and its height cannot exceed any bar in that sequence. ### Function Signature ```python def largest_histogram_rectangle(heights: list[int]) -> int: ``` ### Rules - Empty input has area zero. - A zero-height bar prevents a positive-height rectangle from crossing it. - Return only the maximum area, so tied rectangles require no positional tie-break. ### Constraints These are exercise bounds for the referenced histogram task. - `0 <= len(heights) <= 100000`. - `0 <= heights[i] <= 1000000000`. - The answer can exceed signed 32-bit range. Use 64-bit arithmetic. The maximum is `100000000000000`, below `2^53`. ### Examples ```text Input: heights = [2, 1, 5, 6, 2, 3] Output: 10 ``` The adjacent bars of heights five and six support a rectangle of height five and width two. ```text Input: heights = [4, 4, 0, 3] Output: 8 ``` The first two bars support area eight. The zero-height bar separates them from the final bar.

Constraints

  • `0 <= len(heights) <= 100000`.
  • `0 <= heights[i] <= 1000000000`.
  • The answer can exceed signed 32-bit range. Use 64-bit arithmetic. The maximum is `100000000000000`, below `2^53`.

Examples

Input: ([2, 1, 5, 6, 2, 3],)

Expected Output: 10

Explanation: A rectangle can be wider than the tallest individual bar.

Input: ([4, 4, 0, 3],)

Expected Output: 8

Explanation: Equal heights combine, but a zero splits positive runs.

Hints

  1. For a fixed height, the useful span extends until a smaller bar on either side.
  2. When a shorter bar arrives, some earlier heights can no longer extend to the right.

Find the largest rectangle beneath a histogram

mediumByteDanceSoftware Engineer

Given nonnegative heights of adjacent histogram bars, each of width one, return the largest area of an axis-aligned rectangle that fits entirely beneath the histogram. A rectangle spans a contiguous sequence of bars and its height cannot exceed any bar in that sequence.

Function Signature

def largest_histogram_rectangle(heights: list[int]) -> int:

Rules

  • Empty input has area zero.
  • A zero-height bar prevents a positive-height rectangle from crossing it.
  • Return only the maximum area, so tied rectangles require no positional tie-break.

Constraints

These are exercise bounds for the referenced histogram task.

  • 0 <= len(heights) <= 100000.
  • 0 <= heights[i] <= 1000000000.
  • The answer can exceed signed 32-bit range. Use 64-bit arithmetic. The maximum is 100000000000000, below 2^53.

Examples

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

The adjacent bars of heights five and six support a rectangle of height five and width two.

Input: heights = [4, 4, 0, 3]
Output: 8

The first two bars support area eight. The zero-height bar separates them from the final bar.

Examples

Example 1
Input
([2, 1, 5, 6, 2, 3],)
Output
10
Notes
A rectangle can be wider than the tallest individual bar.
Example 2
Input
([4, 4, 0, 3],)
Output
8
Notes
Equal heights combine, but a zero splits positive runs.

Constraints

  • •`0 <= len(heights) <= 100000`.
  • •`0 <= heights[i] <= 1000000000`.
  • •The answer can exceed signed 32-bit range. Use 64-bit arithmetic. The maximum is `100000000000000`, below `2^53`.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def largest_histogram_rectangle(heights: list[int]) -> int:
Sample call
largest_histogram_rectangle([2, 1, 5, 6, 2, 3])
Language
Case 1
Input
([2, 1, 5, 6, 2, 3],)
Expected
10
Case 2
Input
([4, 4, 0, 3],)
Expected
8

Browse More Questions

Show the approach

Approach

Keep bar indices on a stack with nondecreasing heights. Scan left to right, using a virtual zero-height bar just after the end. When the current bar is shorter than the top bar, pop the top and calculate its rectangle before inspecting the new top. The current position is the first smaller height to the right. The new top marks the previous smaller or equal bar to the left. The width excludes both boundary positions, or starts at index zero if the stack is empty.

Equal heights may temporarily limit the span of a later equal bar, but the earliest equal bar will also be popped and considers the full feasible span. Every positive-height rectangle can be expanded to one of the spans evaluated this way at its minimum bar height. Take the maximum area and promote the multiplication to 64 bits where necessary.

Time complexity:
O(n)
Space complexity:
O(n)