Title: (Stack)
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 the where width of each bar is 1, given height = [2,1,5,6,2,3]
.
The largest rectangle is shown in the shaded area, and which has an area = 10
unit.
For example,
Given height = [2,1,5,6,2,3]
,
Return 10
.
Exercises
Reference http://www.cnblogs.com/lichen782/p/leetcode_Largest_Rectangle_in_Histogram.html
Difficulties, thinking more difficult to think. Look more.
Public classSolution { Public intLargestrectanglearea (int[] height) {Stack<Integer> stack =NewStack<integer>(); int[] h =New int[height.length+1]; intI=0; intresult=0; H= arrays.copyof (height, height.length+1); while(i<h.length) {if(Stack.isempty () | | H[i]>H[stack.peek ()]) Stack.push (i++); Else { intt =Stack.pop (); Result= Math.max (result, h[t]* (Stack.isempty ()? I:i-stack.peek ()-1)); } } returnresult; }}
[Leetcode] Largest Rectangle in histogram