zoukankan      html  css  js  c++  java
  • [LeetCode 84] Largest Rectangle in Histogram

    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.

     

    Example:

    Input: [2,1,5,6,2,3]
    Output: 10
     
     
    Analysis:
    Q: Think about how you would solve this problem by hand?  
    A:  Given the rectangle requirement, for two consecutive heights height[i] and height[i + 1], if height[i + 1] < height[i], we know that the rectangle whose heights are height[i] has just reached their right ends as they can not use height[i + 1] anymore. So we calculate the area of all rectangles whose right ends are index i and heights are height[i] . If height[i + 1] >= height[i], then we do not do any calculations becase we are not sure about the right bound of any rectangles.
     
    This implies maintaining a non-decreasing stack of heights.  We keep pushing bigger or the the same heights into this stack until we hit a smaller height than previous. Then we keep poping heights that are strictly bigger than the current height and calculate rectangle areas until we hit the left boundary(top of the stack is <= the current height).
     
    Repeat the above process until we've iterated all heights.  Any heights left in the stack are non-decreasing, pop these indices out and calcuate their corresponding rectangle areas. To make the calculation easier, instead of storing the actual height in the stack, their indices in the array are stored. This way it is easier to calculate the length of each rectangle by probing the top of the stack.
     
     
    O(n) runtime, O(n) space 
    class Solution {
        public int largestRectangleArea(int[] heights) {
            int ans = 0, i = 0;
            Stack<Integer> stack = new Stack<>();
            stack.push(-1);
            
            while(i < heights.length) {
                while(stack.peek() >= 0 && heights[stack.peek()] > heights[i]) {
                   int j = stack.pop();
                   ans = Math.max(ans, heights[j] * (i - 1 - stack.peek()));
                }
                stack.push(i);  
                i++;
            }
            
            while(stack.peek() >= 0) {
                int j = stack.pop();
                ans = Math.max(ans, heights[j] * (i - 1 - stack.peek()));
            }
            return ans;
        }
    }

     

    Related Problems
     
     
  • 相关阅读:
    2017浙江工业大学-校赛决赛 BugZhu抽抽抽!!
    数据可视化建设是企业战略决策之刚需
    SIMPLE_DEV_PM_OPS宏
    Java连接程序数据源
    ROS(indigo)一个简单灵活和可扩展的2D多机器人仿真器stdr_simulator
    ROS_Kinetic_21 使用Qt Creator Plug in即ros_qtc_plugin
    ROS(indigo)使用Qt Creator Plug in即ros_qtc_plugin
    现代控制理论教学与半年工作总结(未完成待补充)
    企业应该如何运用商业智能
    USB有时adb shell连不上设备
  • 原文地址:https://www.cnblogs.com/lz87/p/7477789.html
Copyright © 2011-2022 走看看