84. Largest Rectangle in Histogram

本文探讨了在给定直方图的情况下寻找最大矩形面积的问题。介绍了两种算法:一种是优化过的O(n^2)暴力求解法,另一种是利用栈实现的O(n)高效算法。栈算法通过维护高度递增的索引序列,巧妙地解决了问题。

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.

For example, 
Given height = [2,1,5,6,2,3], 
return 10.    

分析:最暴力的方法就是枚举所有区间的矩形值,然后选择最大的。可以通过分别枚举区间右边界和区间左边界,时间复杂度O(n^2)。这样做大数据会超时。枚举的过程中可以优化一下:可以很容易理解,如果height[i+1] > height[i] 那么区间[k…i]内的最大矩形肯定不会超过区间[k…i+1]内的最大矩形,比如上例中的区间[0…3]内的矩形要大于[0…2]内的矩形,因为height[3] > height[2]。因此我们在枚举区间右边界时,只选择那些height上升区间的最大值处作为右边界(比如例子中的2 、6 、3)。优化后可以通过leetcode的大数据,虽然做了优化,但是时间复杂度还是O(n^2),代码如下:

class Solution {
public:
    int largestRectangleArea(vector &height) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        int len = height.size(),res = 0;
        int rBorder = 0; //每次选择递增序列的最大值作为右边界
        while(rBorder < len)
        {
            if(rBorder + 1 < len && height[rBorder+1] >= height[rBorder])rBorder++;
            else
            {//找到了右边界
                int minVal = height[rBorder];
                //枚举左边界
                for(int lBorder = rBorder; lBorder >= 0; lBorder--)
                {
                    if(minVal > height[lBorder])
                        minVal = height[lBorder];
                    int tmpArea = minVal * (rBorder - lBorder + 1);
                    if(res < tmpArea)res = tmpArea;
                }
                rBorder++;
            }
        }
        return res;
    }
};

网上找到了一种O(n)的解法,非常巧妙,通过栈来维护height数组中递增的索引,注意代码中的注释:

class Solution {
public:
    int largestRectangleArea(vector<int> &height) {
        height.push_back(0);//数组末尾插入dummy元素0
        int len = (int)height.size(),res = 0;
        stack<int> S;//注意栈内保存的是数组height的下标索引
        for (int i = 0; i < len; i++)
        {
            if (S.empty() || height[i] > height[S.top()]) S.push(i); //注意,这里写为>或者>=均可。
            else
            {
                int tmp = S.top();
                S.pop();
                res = max(res, height[tmp] * (S.empty() ? i : i-S.top()-1));
                i--;
            }
        }
        return res;
    }
};

之前看上面的代码,一直百思不得其解,实在不得要求,后来看了两篇博文才彻底明白。

先看博文http://bangbingsyb.blogspot.com/2014/11/leetcode-largest-rectangle-in-histogram.html,思路1中的Brute Force算法思想为,对每个bar,假设它是它所能参与围成的最大面积矩形中的最短板,这就要求我们使用双指针向两边扫,直到遇到比这个bar短的bar或者到达边界。 然后根据左右指针的距离得到面积。

再次看博文http://www.geeksforgeeks.org/largest-rectangle-under-histogram/,博文中最精髓的一段话是这样的:

For every bar ‘x’, we calculate the area with ‘x’ as the smallest bar in the rectangle. If we calculate such area for every bar ‘x’ and find the maximum of all areas, our task is done. How to calculate area with ‘x’ as smallest bar? We need to know index of the first smaller (smaller than ‘x’) bar on left of ‘x’ and index of first smaller bar on right of ‘x’. Let us call these indexes as ‘left index’ and ‘right index’ respectively.
We traverse all bars from left to right, maintain a stack of bars. Every bar is pushed to stack once. A bar is popped from stack when a bar of smaller height is seen. When a bar is popped, we calculate the area with the popped bar as smallest bar. How do we get left and right indexes of the popped bar – the current index tells us the ‘right index’ and index of previous item in stack is the ‘left index’.

博文告诉我们,使用栈的终极目的是为了维护left index和right index。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值