LeetCode:Largest Rectangle in Histogram(直方图中最大矩形面积)

【leetcode热题100】最大矩形 给定一个仅包含0和1、大小为的二维二进制矩阵,找出只包含1的最大矩形,并返回其面积。6最大矩形如上图所示。01。 阅读详情

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.

求最大矩形的面积

方法一:暴力来leetcode上也能通过,方法也非常的好理解,就是遍历到每一个高度,向左和向右遍历,找到低于该高度的边界顺便计算矩形的宽度,进而计算面积,最终找到面积的最大值。

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        if(heights.size()==0){
            return 0;
        }
        int maxArea=0;//设置初始值
        for(int i=0;i<heights.size();i++){
            int start=i,end=i+1;
            while(start>=0 && heights[start]>=heights[i]){  //找到左边界
                 start--;  
            }
            while(end<heights.size() && heights[end]>=heights[i]){ //找到右边界
                end++;
            }
            maxArea=max(maxArea,heights[i]*(end-start-1));  //计算面积筛选最大值。
        }
        return maxArea;
    }
};

方法二:借用栈来解决问题。维护一个非递减的栈,也就是高度非递减,[2,1,5,6,2,3],高度是非递减的,如下图所示,以B1为高度的矩形,开始位置也就是左边界,就是其本身,因为左边C1高度要比他低,所以不能计算。那怎么确定他的右边界呢,也就是矩形到什么停止呢,这就到了,不能产生非递减栈的那个高度,比如6后面的儿,2<6 不符合非递减栈的要求,这时候也是左边所有矩形结束的位置。这也解释了,为什么在数组的后面添加一个-1,因为要找到最后一个矩形结束的位置。

代码如下:

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        heights.push_back(-1);  //添加到最后,是为了找到最后一个矩形的右边界
        int max_size=0;
        int index=0;
        stack<int> s;  //存储位置

        while(index<heights.size())
        {
            if(s.size()==0||heights[s.top()]<=heights[index])  //维护非递减栈。
            {
                s.push(index);
                index++;
            }
            else
            {
                int top=s.top();  //栈中高度最高的。
                s.pop();
                int size=0;

                if(s.size()==0)  //如果这个时候栈为空,说明到这个位置位置,都不符合非递减栈,所以前面的高度都比他高,所以他的宽度为index.
                {
                    size=heights[top]*index;
                }
                else
                {
                    //如果栈不为空,这个时候他的左边界就是栈顶,因为这个时候它本身已经pop.
                    size=heights[top]*(index-s.top()-1);
                }

                if(size>max_size) max_size=size;  //计算面积
            }
        }

        return max_size;
    }
};

望批评指正。

每天一道LeetCode-----计算直方图中最大矩形的面积 Largest Rectangle in Histogram原题链接Largest Rectangle in Histogram给定一个直方图,计算这个直方图中最大的矩形面积。输入的是直方图中每个柱的高度首先可以想到要求面积就需要确定矩形的高度,但是每个柱子的高度是不一样的,而不同的柱子组成的矩形要以最短的那个作为高那么,是否可以对每个高度的分别计算面积呢?简单的说就是以每个柱子的高度作为最后矩形的 阅读详情

相关推荐

Leetcode85. 最大矩形

题目描述:给定一个仅包含 0 和 1 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。 解法:动态规划 先将矩形对每一列从上到下进行合并,得到 然后再对每一位置的数进行遍历,具体操作如下: 如果arr[i][j] * cols <= max,即即使一整行都符合条件都无法超过最大值,剪枝跳过 count初始化为0,从该位置向前遍历,如果arr[i][j - 1] >= arr[i][j],count++;同理向右遍历,最后得到该位置的最大矩形面积

qq_43556545的博客 431

LeetCode每日一题-最大矩形

题目 给定一个仅包含 0 和 1 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。 分析 这道题一开始想到用动态规划来做,但没有得到正确结果,看了一会其他人的题解,大部分是用力扣84题的方法来解,但实现逻辑较多,不够清晰,所以决定继续用动态规划做这道题。 这道题核心是怎么求一个点涉及到的矩形面积,如下图的点(i,j),它涉及到三个矩形我用三种颜色标记了,接下来只需找出这三种颜色矩形中最大面积就行了。 第一层为黄色,它的面积为这个矩.

zilong00007的专栏 863

LeetCode: 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 wher

亦忘却_亦纪念 1万+

leetcode:84. 柱状图中最大的矩形面积

给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。求在该柱状图中,能够勾勒出来的矩形的最大面积。问题:

OceanStar的博客 1164

从零到一:Cadence AC仿真中的运放性能参数深度解析与实战避坑指南

本文深度解析Cadence AC仿真中运放性能参数的精确测量方法,包括开环增益、带宽和相位裕度等关键指标。通过实战案例分享常见设置错误的避坑技巧,帮助工程师提升设计成功率和仿真效率,特别针对全差分运放和工艺角分析提供专业指导。

www00的博客 294

microblaze之Video Processing Subsystem调试误区

内容:总结video processing subsystem调试中遇到的问题,以及在解决问题中的思路方法论,引为前车之鉴。 1,我的目的是希望采用vps中的scaler模块对图像进行拉伸缩小。IP的配置如下: 2,整个系统的框架图如下,仅比上期的内容增加了scaler模块,如下: 3,查手册,可以知道这个scaler only组件分成了hscale,vscale和GPIO,其中GPIO是控制上下游stream流控设备复位用,暂时忽略。 hscale和vscale的寄存器空间如下,主要关注图

YDY5659150的专栏 3555

[LeetCode]—Largest Rectangle in Histogram 求直方图最大填充矩形面积

Largest Rectangle in Histogram

清水 977

单片机技术入门与实战:从零基础到项目开发

单片机(MCU)作为嵌入式系统的核心组件,集成了CPU、内存和各种接口电路,广泛应用于智能家居、工业控制和汽车电子等领域。其工作原理基于实时控制和低功耗设计,通过定时器、中断系统和通信接口等功能模块实现各种控制任务。在物联网和智能硬件快速发展的背景下,掌握单片机开发技术成为电子工程师的重要技能。本文以STM32和PIC系列为例,详细解析单片机的基本概念、功能模块和选型指南,并分享从入门到项目实战的系统学习方法,帮助开发者在3-6个月内快速掌握这一关键技术。

weixin_35753291的博客 653

LeetCode(084)-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 ea...

Fly_Fly_Zhang的博客 691

自绘CListCtrl时发现ON_WM_MEASUREITEM_REFLECT和DrawItem(LPDRAWITEMSTRUCT lpMeasureItemStruct) 无响应

自绘CListCtrl时发现ON_WM_MEASUREITEM_REFLECT和DrawItem(LPDRAWITEMSTRUCT lpMeasureItemStruct) 无响应 一般情况下,我们都是在 dialog 里面 放一个 list 控件,然后 用自己的类绑定自己的自绘对象,如同下面: 1.  在 dialog 里面 加一个 list 列表 2. 然后 绑定这个列表 哈哈...

mfreesky的专栏 2171

深入解析UVM寄存器模型:reg2bus与bus2reg的隐式调用机制

本文深入解析了UVM寄存器模型中reg2bus与bus2reg函数的隐式调用机制。通过追踪一次寄存器访问的完整旅程,揭示了这两个关键函数是如何在uvm_reg_map和uvm_reg_predictor中被框架自动调用的,从而在抽象的寄存器操作与具体总线事务之间完成转换,并实现镜像值的自动更新。掌握此机制是高效调试和灵活应用UVM寄存器模型的基础。

weixin_29211309的博客 441

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 ...

andersonxie的博客 2800

第33次CCF-CSP认证考试 第一题 词频统计 满分题解

本人小白一枚,抱着学习的态度想到网上找一些csp的参考答案,但由于实力有限,大佬的代码大都看不太懂,于是萌生了想用一些简单,基础的语句完成csp前两题的想法并公开,以便与像我这样实力不强的同学交流学习,如有更好的建议,还请斧正,感谢!

qq_75242106的博客 1869

LeetCode | 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 o

solar一抹阳光的专栏 1137

【数据集】30 m地表温度LST数据集

【数据集】30 m地表温度LST数据集

WW、forever的博客 1131

SAR图像降噪:基于小波变换和双边滤波器的Matlab实现

小波变换和双边滤波器是常用的图像降噪技术,它们可以在保持图像细节的同时有效抑制噪声。本文介绍了基于小波变换和双边滤波器的SAR图像降噪方法,并提供了相应的Matlab源代码。通过对比原始图像和降噪后的图像,可以观察到噪声得到一定程度的抑制,同时保留了图像的细节信息。本文将介绍一种基于小波变换和双边滤波器的方法,用于SAR图像的降噪,并提供相应的Matlab源代码。希望这篇文章能够帮助您理解基于小波变换和双边滤波器的SAR图像降噪方法,并为您提供实现该方法的Matlab源代码。对图像的高频部分进行降噪处理。

ScriptCharm的博客 840

【leetcode每日刷题】Largest Rectangle in Histogram

https://leetcode.com/problems/largest-rectangle-in-histogram/ 求直方图中的最大矩形 从左到右找到最高的矩形,然后从右向左计算最大的面积。 class Solution(object): def largestRectangleArea(self, heights): """ :type h...

菜菜鸟的博客 173

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

最大矩形(Stack,Vector,Queue,Deque)

Largest Rectangle in Histogram:https://leetcode.com/problems/largest-rectangle-in-histogram/description/public int largestRectangleArea(int[] height) { int len = height.length; Stack&l...

heiheibupt的博客 216

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Leetcode -- Maximal Rectangle

https://oj.leetcode.com/problems/maximal-rectangle/ Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area  public int maximalRectan

chaochen1407的专栏 388

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

算法设计与分析第一周练习

题目 Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find t...

Vilin的博客 282

LeetCode_直方图最大面积(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

GitHubClub 8010

Leetcode. Largest Rectangle in Histogram

1. 题解 题目 链接🔗:84. Largest Rectangle in Histogram 这道题和42. Trapping Rain Water有些相像,但是42题求的是能围住的最大面积,而本题求的是连成的矩阵的最大面积,因而在计算面积的方法上有些许不同。85. Maximal Rectangle就是在本题基础上的变式,通过一系列变化可以使用本题方法。 分析 简单分析可以知道最大矩形面积有两种情况: 当前矩形的高度heights[i] * 1 当前矩形的高度heights[i] * (向右第一个比

三明治文鱼的博客 7797

[C++]LeetCode: 133 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

cindy_niu的专栏 3023

leetcode_question_85 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

ZkvIA的博客 1万+

leetcode 随笔 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...

Runner_of_nku的博客 1万+

84. Largest Rectangle in Histogram 柱状图中的最大矩形

LeetCode传送门求柱状图(Histogram)中的最大矩形(Largest Rectangle),是一道ACM比赛题,又是一道Leetcode题,还是一道经常被问的面试题。如下图所示,可以将柱状图表示为一个数组[2,1,5,6,2,3],每个柱子的宽度假设是一个单位。求出的最大的矩形应该如下斜线所示,大小是10。最容易想到的解法是brute forceclass Solution: ...

头戴三叉束发紫金冠体挂西川红棉百花袍身披兽面吞头连环铠腰系勒甲玲珑狮蛮带手持方天画戟坐下嘶风赤兔马是也 2421
上一篇: LeetCode:Longest Palindromic Substring(最长回文字串)
下一篇: 二叉树前序遍历非递归
Raven_csdn
博客等级 码龄8年 26粉丝 69原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值