class Solution { public int largestRectangleArea(int[] heights) { Stack<Integer> stack=new Stack<Integer>(); int maxArea=0; for(int i=0;i<=heights.length;i++) { int h=i<heights.length?heights[i]:0; if(stack.isEmpty()||heights[stack.peek()]<=h) stack.push(i); else { int idx=stack.pop(); maxArea=Math.max(maxArea, heights[idx]*(stack.isEmpty()?i:i-stack.peek()-1)); i--; } } return maxArea; } }