classSolution{publicintlargestRectangleArea(int[] heights){int[] newHeight =newint[heights.length +2];System.arraycopy(heights,0, newHeight,1, heights.length);
newHeight[heights.length+1]=0;
newHeight[0]=0;Stack<Integer> stack =newStack<>();
stack.push(0);int res =0;for(int i =1; i < newHeight.length; i++){while(newHeight[i]< newHeight[stack.peek()]){int mid = stack.pop();int w = i - stack.peek()-1;int h = newHeight[mid];
res =Math.max(res, w * h);}
stack.push(i);}return res;}}