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 .
Has you met this question in a real interview? Solution:
1 Public classSolution {2 Public intLargestrectanglearea (int[] height) {3 if(height.length==0)return0;4 5Stack<integer> index =NewStack<integer>();6Index.push (-1);7 intMax = 0;8 for(inti=0;i){9 while(Index.peek ()!=-1){Ten if(Height[index.peek ()]>Height[i]) { One intCur =Index.pop (); Amax = Math.max (max,height[cur]* (I-index.peek ()-1)); -}Else Break; - } the Index.push (i); - } - - while(Index.peek ()!=-1){ + intCur =Index.pop (); -max = Math.max (max,height[cur]* (Height.length-index.peek ()-1)); + } A at returnMax; - } - -}
Leetcode-largest Rectangle in histogram