84. Largest Rectangle in Histogram

84. Largest Rectangle in Histogram


https://www.youtube.com/watch?v=KkJrGxuQtYo&t=129s

https://leetcode.com/problems/largest-rectangle-in-histogram/solution/


https://www.cnblogs.com/boring09/p/4231906.html

https://www.youtube.com/watch?v=ZmnqCZp9bBs




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.


Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
 


The largest rectangle is shown in the shaded area, which has area = 10 unit.



Input: [2,1,5,6,2,3]
Output: 10

Solution 1 : time o(n^ 2) 

    // loop thru from left to right
        // if the height is increasing, keep i++;
        // and once the height is decreasing, we look back at the 
        // columns and calculate every area 
        // formula is length * min(heights)
        // use a var called res to update the res 
        // after done with that, i++
        // if the height is decreasing again, we do the same as above 
     
            // check if the next one is increasing, or decreasing
            // if increasing , keep going
            // if dereasing or hit the end, reflect and calculate prev areas
            




class Solution {
    public int largestRectangleArea(int[] heights) {
      if(heights == null || heights.length == 0) return 0;
      
      int res = 0;
      for(int i = 0; i < heights.length; i++){
        // if hit the end or the next height is decreasing
        if(i == heights.length - 1 || heights[i] > heights[i + 1]){
          int minHeight = heights[i];
          for(int j = i; j >= 0; j--){
            minHeight = Math.min(minHeight, heights[j]);
            int area = (i - j + 1) * minHeight;
            res = Math.max(res, area);
          }
        }
      }
      return res;
    }
}



Solution 2 : using stack , time o(n) 

https://www.youtube.com/watch?time_continue=17&v=RVIh0snn4Qc


https://www.cnblogs.com/yrbbest/p/4437139.html


https://blog.csdn.net/crystal6918/article/details/51946334


https://blog.csdn.net/qq508618087/article/details/50336795

https://www.geeksforgeeks.org/largest-rectangle-under-histogram/
https://www.youtube.com/watch?v=MhQPpAoZbMc


https://www.youtube.com/watch?v=ZmnqCZp9bBs


找左右边界, 这个题要过个例子才行, 自己手动过例子




// others code 
class Solution {
    public int largestRectangleArea(int[] heights) {
        if (heights == null || heights.length == 0) {
            return 0;
        }
        Stack<Integer> stack = new Stack<Integer>();
        int ans = 0;
        for (int i = 0; i <= heights.length; i++) {
            int cur = (i == heights.length) ? -1 : heights[i];
            while (!stack.isEmpty() && heights[stack.peek()] >= cur) {
                int h = heights[stack.pop()];
                int w = stack.isEmpty() ? i : i - stack.peek() - 1;
                ans = Math.max(ans, h * w);
            } 
            stack.push(i);
        }
        return ans;
    }
}

 


class Solution {
    public int largestRectangleArea(int[] heights) {
        int res = 0;
        for(int index = 0; index < heights.length; index++){
            int height = heights[index];
            int width = helper(heights, index);
            int area = height * width;
            res = Math.max(res, area);
        }
        return res;
    }
    private int helper(int[] heights, int index){
        int i = index;
        int j = index;
        while(i >= 0 && heights[i] >= heights[index]){
            i--;
        }
        while(j < heights.length && heights[j] >= heights[index]){
            j++;
        }
        return j - i - 1;
    }
}

 

 

 

posted on 2018-09-20 18:18  猪猪&#128055;  阅读(133)  评论(0)    收藏  举报

导航