classSolution{publicintlargestRectangleArea(int[] heights){// 单调栈,这个题不好理解。int len = heights.length;//int[] newHeights =newint[len+2];for(int i =0; i < len; i++){
newHeights[i+1]= heights[i];}
len = len+2;Stack<Integer> stack =newStack<>();
stack.push(0);int sum =0;for(int i =1; i < len; i++){if(newHeights[i]> newHeights[stack.peek()]){
stack.push(i);}elseif(newHeights[i]== newHeights[stack.peek()]){
stack.pop();
stack.push(i);}else{while(!stack.isEmpty()&& newHeights[i]< newHeights[stack.peek()]){int num = stack.peek();
stack.pop();if(!stack.isEmpty()){// 右减去左int w = i - stack.peek()-1;// 中间的。int h = newHeights[num];
sum =Math.max(sum, w*h);}}
stack.push(i);}}return sum;}}