84. Largest Rectangle in Histogram

本文介绍了一种计算直方图中最大矩形面积的有效算法。通过使用栈来跟踪直方图条的高度,该算法能在O(n)的时间复杂度内解决问题。文中详细解释了算法流程,包括如何处理上升和下降趋势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

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 heights = [2,1,5,6,2,3],
return 10.

分析

计算直方图能围成的最大矩形,可以用分治思想解决,即构成最大矩形的直方图在当前范围的左侧,右侧,或者跨过中线,只需给出跨过中线的解决方法即可,但这一方法在平台上提交不过,因为有些变量在一个进程重复调用,旧值遗留,新的方法参考:My concise C++ solution, AC 90 ms,先将一个为0的值压入高度向量中,然后用index记录便利过得高度索引,当遇到上升时直接压入index,一旦遇到小于最后一个index高度时,开始处理下降趋势,找到index中保存的所有大于等于当前高度的直方图所围成的最大矩形,并将计算过的直方图索引从index弹出,index中只保留小于当前处理的直方图高度的索引,由于最后一个高度是0,所以一定能够计算到当前所有直方图高度围成的最大矩形。

索引记录方法:

class Solution {
    public:
        int largestRectangleArea(vector<int> &height) {
            
            int ret = 0;
            height.push_back(0);
            vector<int> index;//index记录遇到的元素下标
            
            for(int i = 0; i < height.size(); i++)
            {
                //当直方图高度不断增加时,一直向里面压入高度,一旦遇到下降,则在循环中计算当前高度之前围成的矩形最大面积,即index中比当前高度高或相等的都会弹出,用于计算之前的最大面积,而index中比当前高度小的保留下来,例如[2,5,6,7,3],由于最初压入一个0,变成[2,5,6,7,3,0],前4个直接压入index,遇到3时,计算[5,6,7]围成的最大面积,index中保留2的索引,最后遇到0,则计算[2,5,6,7,3]围成的最大面积
                while(index.size() > 0 && height[index.back()] >= height[i])
                {
                    int h = height[index.back()];
                    index.pop_back();
                    
                    int sidx = index.size() > 0 ? index.back() : -1;
                    if(h * (i-sidx-1) > ret)
                        ret = h * (i-sidx-1);
                }
                index.push_back(i);
            }
            
            return ret;
        }
    };
分治法:

class Solution {
public:
    int leftORright(vector<int> heights,int beg,int end){//计算左右区间的最大矩形
        int mid=(beg+end)/2;
        if(beg<end){
            int left=leftORright(heights,beg,mid);
            int right=leftORright(heights,mid+1,end);
            int cross=crossMid(heights,beg,end);
            return max(left,right)>cross?max(left,right):cross;
        }
        else
            return heights[beg];
    }
    int crossMid(vector<int> heights,int beg,int end){//计算跨过中线的最大矩形,其中leftIndex直接对应mid,rightIndex对应mid+1
        int mid=(beg+end)/2;
        if(beg<end){
            int leftIndex=mid;
            int rightIndex=mid+1;
            int crossMax=0,crossMin=heights[mid];
            while(leftIndex>=beg&&rightIndex<=end){
                crossMin=min(heights[leftIndex],crossMin);
                crossMin=min(heights[rightIndex],crossMin);
                crossMax=max(crossMax,crossMin*(rightIndex-leftIndex+1));
                --leftIndex;
                ++rightIndex;
            }
            while(leftIndex>=beg){
                crossMin=min(crossMin,heights[leftIndex]);
                crossMax=max(crossMax,crossMin*(rightIndex-leftIndex));
                --leftIndex;
            }
            while(rightIndex<=end){
                crossMin=min(crossMin,heights[rightIndex]);
                crossMax=max(crossMax,crossMin*(rightIndex-leftIndex));
                ++rightIndex;
            }
            return crossMax;
        }
        else
            return heights[beg];
    }
    int largestRectangleArea(vector<int>& heights) {
        if(heights.empty())
            return 0;
        if(heights.size()==1)
            return heights[0];
        int mid=heights.size()/2;
        int leftMax=leftORright(heights,0,mid);
        int rightMax=leftORright(heights,mid+1,heights.size()-1);
        int crossMax=crossMid(heights,0,heights.size()-1);
        
        return max(leftMax,rightMax)>crossMax?max(leftMax,rightMax):crossMax;
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值