Largest Rectangle in Histogram

Given _n _non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

Above is a histogram where width of each bar is 1, given height =[2,1,5,6,2,3].

The largest rectangle is shown in the shaded area, which has area =10unit.

Example

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

Output:
 10

Note

单调栈严格递增栈,可以跑一下test case:

height

2

6

5

3

2

1

width

1

1=4-2-1

2=4-1-1

1=6-4-1

4=6-1-1

6

i

1

4

4

6

6

6

s.peek()

null

2

1

4

1

null

res

2

6

10

3

8

6

Code

Last updated