LeetCode84. Largest Rectangle in Histogram

very easy to understand problem:
Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

classic mono stack problem.
for evert fixed value, we need to find the ple and nle of current value, store the distance to calculate the width,
so the maximum area which based on this fixed value will be width*height = ple[i]*nle[i]*nums[i]. and we need to keep record of global maximum of this area.

now, the following code is modified by LC907, and it is accepted.

class Solution {
    public int largestRectangleArea(int[] heights) {
        if (heights == null || heights.length == 0) return 0;
        Stack<int[]> ple = new Stack<>();//use stack
        Stack<int[]> nle = new Stack<>();
        int[] left = new int[heights.length];
        int[] right = new int[heights.length];
        int res = 0;//
        //construct ple
        for (int i = 0; i<heights.length; i++) {
            while (!ple.isEmpty() && ple.peek()[0] >= heights[i]) { //keep pop until find the first one that peek() < height[]
                ple.pop();
            }
            left[i] = ple.isEmpty()? i+1: i - ple.peek()[1]; //peek() is the first one that smaller than current A[i], use a simple instance to get the i+1 and i-ple.peek()[1] part
            ple.push(new int[]{heights[i], i});
        }
        
        //construct nle
        for (int i = heights.length - 1; i>=0; i--) {
            while (!nle.isEmpty() && nle.peek()[0] >= heights[i]) { 
                nle.pop();
            }
            right[i] = nle.isEmpty() ? heights.length - i: nle.peek()[1] - i; 
            nle.push(new int[]{heights[i], i});
            
        }
        for (int i = 0; i<heights.length; i++) {
            res = Math.max(res, (left[i] - 1 + right[i] -1 + 1) * heights[i]); //pay attention here
        }
        return res;
    }
}

and of course I used to using one stack to solve this problem:
the solution is essentally solved in mono stack.
and each time we fixed current value as the last, and we only maintains ple.

class Solution {
    public int largestRectangleArea(int[] heights) {
        if (heights == null || heights.length == 0) return 0;
        Stack<Integer> stack = new Stack<>();//use stack
        int res = 0;//
        for (int i = 0; i <= heights.length; i++) {//iterate heights array
            int h = i == heights.length ? 0 : heights[i];//如果i是height.length 就设h为0 否则设为height[i]
            while (!stack.isEmpty() && h < heights[stack.peek()]) {//当stack非空 并且  h < height[stack.peek()]
                int height = heights[stack.pop()];//如果遇到小的值 就将之Pop出来
                int start = stack.isEmpty() ? -1 : stack.peek(); //计算的起始index
                int area = height * (i - start - 1);//计算面积 高*宽
                res = Math.max(res, area);//维护最大值
            }
            stack.push(i);//stack里面存的是Index
        }
        return res;
    }
}
posted @ 2020-05-26 12:43  EvanMeetTheWorld  阅读(16)  评论(0)    收藏  举报