LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
题目描述
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。

以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]。

图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。
示例:
输入: [2,1,5,6,2,3]
输出: 10
解题思路
这道题跟LeetCode 11很相似,但是因为考虑柱子宽度,因此解题技巧不相同,此题考查单调栈的应用。我们首先在数组最后加入0,这是为了方便处理完数组中的所有高度数据。假设存储高度坐标的栈为stack,当前处理的高度坐标为i(i∈[0→n]):
- 如果当前stack为空,或者heights[i]大于等于栈顶坐标对应高度,则将i加入stack中,并将i加一;
- 如果heights[i]小于栈顶坐标对应高度,说明可以开始处理栈内的坐标形成的局部递增高度,以求得当前最大矩形面积。弹出当前栈顶坐标 = top,此时栈顶新坐标 = top',那么对应计算面积的宽度w = i - 1 - top'(若弹出栈顶坐标后,stack为空,则对应w = i),得到面积s = heights[top] * w,再次返回2检查栈;
- 遍历完成i∈[0→n],返回最大矩形面积。
代码
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
stack<int> s;
int i = , maxArea = ;
heights.push_back();
while(i < heights.size()){
if(s.empty() || heights[s.top()] <= heights[i]){
s.push(i);
i++;
}
else{
int top = s.top();
s.pop();
int l = s.empty() ? i : i - s.top() - ;
maxArea = max(maxArea, heights[top] * l);
}
}
return maxArea;
}
};
参考自
LeetCode 84. Largest Rectangle in Histogram(直方图中最大矩形面积)
LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)的更多相关文章
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
84. 柱状图中最大的矩形 84. Largest Rectangle in Histogram
- [Swift]LeetCode84. 柱状图中最大的矩形 | Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- Java实现 LeetCode 84 柱状图中最大得矩形
84. 柱状图中最大的矩形 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的 ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
- leetcode 84. 柱状图中最大的矩形 JAVA
题目: 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高 ...
- [LeetCode] 84. 柱状图中最大的矩形
题目链接 : https://leetcode-cn.com/problems/largest-rectangle-in-histogram/ 题目描述: 给定 n 个非负整数,用来表示柱状图中各个柱 ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
随机推荐
- Qt调用VS生成的dll
预备知识: 1.如果在没有导入库文件(.lib),而只有头文件(.h)与动态链接库(.dll)时,我们才需要显示调用,如果这三个文件都全的话,我们就可以使用简单方便的隐式调用. 2.通常Windo ...
- win10下 安装迅雷精简版
下载链接:https://files-cdn.cnblogs.com/files/del88/ThunderMini_1.5.3.288.zip 他妈的 今天安装迅雷精简版 在win10上 竟然报错, ...
- nodeJS中使用mongoose模块操作mongodb数据库
在实际运用中,对于数据库的操作我们不可能一直在cmd命令行中进行操作,一般情况下需要在node环境中来操作mongodb数据库,这时就需要引入mongoose模块来对数据库进行增删改查等操作. 首先, ...
- centeros7安装mysql
转载自:https://www.linuxidc.com/Linux/2016-09/135288.htm 安装之前先安装基本环境:yum install -y perl perl-Module-Bu ...
- 如何查找SAP Fiori launchpad Designer的准确路径即url地址
比如我们知道在SPRO里下面这个路径的customizing activity里打开Fiori Launchpad designer: SAP Netweaver->UI technologie ...
- 8.Mapper动态代理
在前面例子中自定义 Dao 接口实现类时发现一个问题:Dao 的实现类其实并没有干什么 实质性的工作, 它仅仅就是通过 SqlSession 的相关 API 定位到映射文件 mapper 中相应 id ...
- Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)
详见:https://www.w3cschool.cn/spring_cloud/spring_cloud-jl8a2ixp.html 上一篇文章,留了一个悬念,Config Client 实现配置的 ...
- phpstorm 习惯设置
phpstorm 习惯设置 1. 字体:Source Code Pro 大小:14 链接: https://pan.baidu.com/s/1HLpbduBHFvbq1a10QV4uCg 提取码: y ...
- 2-1 bash基本特性
bash基本特性 bash基本介绍 bash是shell的一种,shell是计算机与用户交互的主要接口,狭义上的shell指的是CLI(command line interface命令行接口),用户输 ...
- 04_Redis_Hash命令
一:Redis 哈希(Hash) 1.1:Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象. 1.2:Redis 中每个 hash 可以存储 ...