└── java /java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int largestRectangleArea(int[] heights) { 3 | Stack stack = new Stack<>(); 4 | int n = heights.length; 5 | int maxArea = 0; 6 | for(int i = 0; i <= n; i++){ 7 | int currHeight = (i == n) ? 0 : heights[i]; 8 | while(!stack.isEmpty() && currHeight < heights[stack.peek()]){ 9 | int height = heights[stack.pop()]; 10 | int width = stack.isEmpty() ? i : (i - stack.peek() - 1); 11 | maxArea = Math.max(maxArea, height * width); 12 | } 13 | stack.push(i); 14 | } 15 | return maxArea; 16 | } 17 | } 18 | --------------------------------------------------------------------------------