MentorNode
Start free
Monotonic Stack & Top-K Heap/hard

Largest Rectangle in Histogram

monotonic-stacklargest-rectangle-in-histogram

Largest Rectangle in Histogram

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

Example 1

Input: heights = [2,1,5,6,2,3]
Output: 10
Explanation: The largest rectangle is shown in the middle with heights 5 and 6, area = min(5,6) * 2 = 10.

Constraints

  • 1 <= heights.length <= 10^5
  • 0 <= heights[i] <= 10^4

Loading workspace editor...
Ready to run
Input:
{
  "heights": [
    2,
    1,
    5,
    6,
    2,
    3
  ]
}
Expected Output:
10