题解: - 力扣(LeetCode)
class Solution {
public int largestRectangleArea(int[] heights) {
Stack<Integer> stack = new Stack<>();
int maxArea = Integer.MIN_VALUE;
for(int i = 0;i < heights.length;i++){
int curHeight = heights[i];
while(!stack.empty() && curHeight < heights[stack.peek()]){
int index = stack.pop();
int left = (stack.empty()) ? 0 : stack.peek()+1;
int tempArea = heights[index] * (i - left);
maxArea = Math.max(maxArea,tempArea);
}
stack.push(i);
}
while(!stack.empty()){
int index = stack.pop();
int left = (stack.empty()) ? 0 : stack.peek()+1;
int tempArea = heights[index] * (heights.length - left);
maxArea = Math.max(maxArea,tempArea);
}
return maxArea;
}
}