610. Largest Rectangle in Histogram

MediumStackMonotonic StackStack

Given the heights of adjacent bars of equal width 1, return the area of the largest rectangle that fits entirely under the histogram outline. The input is JSON {heights}.

Input: JSON {heights}.

Output: Integer — the largest rectangle area.

Examples

Example 1
Input: {"heights":[2,1,5,6,2,3]}
Output: 10
Explanation: Bars 5 and 6 form a 5x2 rectangle.
Example 2
Input: {"heights":[2,4]}
Output: 4
Explanation: Bar 4 alone.
Example 3
Input: {"heights":[0]}
Output: 0
Explanation: Zero height.

Constraints

Asked by

FlipkartAmazonGoogleMicrosoftBloombergAccenture
Solve this problem in the editor →