直方图中的最大矩形

在2017CSP认证和蓝桥杯均出现

问题描述:

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.



算法思想:
①遍历数组,每找到一个局部峰值就向前遍历所有值,算出共同矩形的面积,每次对比保留最大值

代码:
// Pruning optimize
class Solution {
public:
    int largestRectangleArea(vector<int> &height) {
        int res = 0;
        for (int i = 0; i < height.size(); ++i) {
            if (i + 1 < height.size() && height[i] <= height[i + 1]) {
                continue;
            }
            int minH = height[i];
            for (int j = i; j >= 0; --j) {
                minH = min(minH, height[j]);
                int area = minH * (i - j + 1);
                res = max(res, area);
            }
        }
        return res;
    }
};


②使用堆栈将复杂度降到最低:时间复杂度--高效率 和空间复杂度--栈开销 均为O(n)
维护一个栈,用来保存递增序列,相当于上面那种方法的找局部峰值,当当前值小于栈顶值时,取出栈顶元素,然后计算当前矩形面积,然后再对比当前值和新的栈顶值大小,若还是栈顶值大,则再取出栈顶,算此时共同矩形区域面积,照此类推,可得最大矩形。

代码:
class Solution {
public:
    int largestRectangleArea(vector<int> &height) {
        int res = 0;
        stack<int> s;
        height.push_back(0);
        for (int i = 0; i < height.size(); ++i) {
            if (s.empty() || height[s.top()] < height[i]) s.push(i);
            else {
                int cur = s.top();
                s.pop();
                res = max(res, height[cur] * (s.empty() ? i : (i - s.top() - 1)));
                --i;
            }
        }
        return res;
    }
};


优化的简洁代码:
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int res = 0;
        stack<int> s;
        heights.push_back(0);
        for (int i = 0; i < heights.size(); ++i) {
            while (!s.empty() && heights[s.top()] >= heights[i]) {
                int cur = s.top(); s.pop();
                res = max(res, heights[cur] * (s.empty() ? i : (i - s.top() - 1)));
            }
            s.push(i);
        }
        return res;
    }
};


③想找出最大矩形的左边界和右边界,把矩形的左边界从数组的左边想右边移动,每移动一次求一次最大矩形面积,最后对所有 矩形面积求最大值。

#include <iostream>    
using namespace std;    
    
int solve(int hist[], int n)    
// hist: contains the heights of the bars     
// n: the number of the bars in the histogram.    
{    
        int* arr = new int[n];// 申请一个额外的数组    
        arr[0] = hist[0];    
        int max = hist[0]; // 最大面积    
    
        for(int i=1; i<n; i++)    
        {    
                arr[i] = hist[i];    
                for(int j=i-1; j>=0; j--)    
                        if(arr[j]>arr[i]){    
                                if(arr[j]*(i-j)>max)    
                                        max = arr[j]*(i-j);    
                                arr[j] = arr[i];    
                        }    
                        else break;    
                //数组arr里的元素,保持非递减的顺序。    
        }    
    
        //重新扫描一边,以更新最大面积    
        for(int i=0; i<n; i++)    
                if(arr[i]*(n-i)>max)    
                        max = arr[i]*(n-i);    
        return max;    
}    
    
int main(int argc, char** argv)    
{    
        int hist[] = {2,1,5,6,2,3};    
        int max = solve(hist, sizeof(hist)/sizeof(int));    
        cout<<max<<endl;    
}



参考自:http://www.cnblogs.com/grandyang/p/4322653.html
http://blog.csdn.net/jiyanfeng1/article/details/8067265?locationNum=6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值