代码随想录训练营Day60|84. 柱状图中最大的矩形

84. 柱状图中最大的矩形

class Solution {
    public int largestRectangleArea(int[] heights) {
        int res=0;
        // 数组扩容,在头和尾各加入一个元素
        int [] newHeights = new int[heights.length + 2];
        newHeights[0] = 0;
        newHeights[newHeights.length - 1] = 0;
        for (int index = 0; index < heights.length; index++){
            newHeights[index + 1] = heights[index];
        }

        heights = newHeights;
        Stack st = new Stack<>();
        st.push(0);

        for(int i=1;i heights[st.peek()]){
                st.push(i);
            }
            else if(heights[i]==heights[st.peek()]){
                st.pop();
                st.push(i);
            }
            else{
                while(!st.isEmpty() && heights[i]

代码随想录训练营Day60|84. 柱状图中最大的矩形_第1张图片

 

你可能感兴趣的:(算法,java,leetcode)