 All Problems
Largest Rectangle in Histogram
hard
stack
arrays
monotonic stack
amazon
google
microsoft
facebook

Given an array of integers heights representing the histogram's bar heights where the width of each bar is 1, return the area of the largest rectangle in the histogram.

Example 1:

Input: 2 1 5 6 2 3
Output: 10
Explanation: The largest rectangle has area 10 (bars at index 2-3, height 5, width 2).

Example 2:

Input: 2 4
Output: 4

Constraints:

  • 1 ≤ heights.length ≤ 10⁵
  • 0 ≤ heights[i] ≤ 10⁴

Input format: A single line of space-separated integers.

Output format: A single integer.

Run to check your code against the sample cases, or submit to run every case