84.柱状图的最大矩形
84. 柱状图中最大的矩形 - 力扣(LeetCode)
注意首尾加0的细节就可
class Solution {
public int largestRectangleArea(int[] heights) {
Deque<Integer> stack = new LinkedList<>();
int[] newHeight = new int[heights.length + 2];
System.arraycopy(heights, 0, newHeight, 1, heights.length);
newHeight[heights.length+1] = 0;
newHeight[0] = 0;
int res = 0;
for(int i = 0;i<newHeight.length;i++){
while(!stack.isEmpty()&&newHeight[i]<newHeight[stack.peek()]){
int right = i;
int mid = stack.pop();
int left = stack.peek();
int h = newHeight[mid];
int w = right-left-1;
res = Math.max(res,h*w);
}
stack.push(i);
}
return res;
}
}