Largest Rectangle in Histogram
Last updated
Last updated
Given nnon-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 =10
unit.
For example,
Given heights =[2,1,5,6,2,3]
,
return10
.
分析
感觉histogram就是左边高度一直往右维持到不能再维持。所以维护递增栈。
递增的栈,每次遇到比当前高的就压入,短的就开始不断弹出栈,并计算面积。直到遇到比它还短的,继续递增。
每次计算面积时候,算到左边第一个比它小的。比如6就是5, 2就是1.width = i - s.peek() - 1
此处i是top下一个数,而s.peek()就是第一个比它小的数。
最后加入高度为0,好让前面的元素出栈。比如123,一直递增没有递减无法出栈。加入0后直接width = i
int h = (i == n ? 0 : heights[i]);
heights[temp] * (s.isEmpty() ? i : i - s.peek() - 1)
算中间有几条线段,可以直接i-j
找左右边界,so bar[i] must be the right boundary (exclusive) of the rectangle, and the previous bar in the stack is the first one that is shorter than the popped one so it must be the left boundary (also exclusive). Then we find the rectangle.
注意这里heights append 0和stack加入-1的做法。