leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html
题目链接 leetcode Largest Rectangle in Histogram 单调栈
对于每一个长条都向前找包含这个长条的最大面积使可行的,但是时间复杂度是O(n^2)大数据会超时。经过观察发现并不需要对每一个长条都向前查找,对于height[i],如果height[i+1]>height[i],那么就没有必要向前查找,原因是可以从height[i]查找到的最大面积向后延伸一格,那么一定大于当前所查找到的面积。因此我们维护一个单调递增栈(严格来说是单调非减),当发现当前的高度小于栈顶元素时,弹栈,并计算最大面积,直到栈顶元素小于当前的高度,把当前的高度压入栈中。
需要注意的有一下几点:
1.不要忘记最扁的长方形面积,实现方法是在height中push_back一个高度为0的长条。这样不会影响最终结果而且可以保证最后一次计算把栈弹空。
2.如果height[i] == height[i+1]时,我们仍然可以把height[i]向后延伸,因此并不弹出height[i],直接压入height[i+1]。
3.在弹出若干个height小于height[i]元素后,当把height压入栈中的时候,并不是把i当做index压入,而是把最后一个被弹出的index压入,因为在下一次计算面积时这些大于height[i]的长条已经不在栈中了,因此我们需要改变index。
4.每次弹栈时,都要计算最大的面积。
代码如下:
1 class Solution { 2 public: 3 int largestRectangleArea(vector<int> &height) { 4 height.push_back(0); 5 stack<pair<int, int> > h;//height, index 6 int res = 0; 7 for( int i = 0 ; i < height.size() ; i++ ) 8 { 9 pair<int, int> tmp; 10 if( h.size() == 0 || h.top().first <= height[i]) 11 { 12 h.push(make_pair(height[i], i)); 13 } 14 else 15 { 16 while(h.size() > 0 && h.top().first > height[i]) 17 { 18 tmp = h.top(); 19 h.pop(); 20 res = max(res, tmp.first * (i-tmp.second)); 21 } 22 h.push(make_pair(height[i], tmp.second)); 23 } 24 } 25 height.pop_back(); 26 return res; 27 } 28 };
View Code
转载于:https://www.cnblogs.com/jostree/p/4052343.html
leetcode Largest Rectangle in Histogram 单调栈相关推荐
- leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题
Largest Rectangle问题 题目描述 Given n non-negative integers representing the histogram's bar height where ...
- [leetcode]Largest Rectangle in Histogram @ Python
原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...
- LeetCode 84. Largest Rectangle in Histogram
LeetCode 84. Largest Rectangle in Histogram Solution1:我的答案 循环里头套了一个动态规划,缺点是当heights个数或最大高度多高时会很耗时间!! ...
- leetcode 84. Largest Rectangle in Histogram | 84. 柱状图中最大的矩形(单调栈)
题目 https://leetcode.com/problems/largest-rectangle-in-histogram/ 题解 一句话总结:遍历数组,对于每个height[i],以其自身的高度 ...
- POJ2559 Largest Rectangle in a Histogram(单调栈)
题意: 给出一组矩形的高,求最多能拼成矩形的最大面积,看图就很清楚了. 要点: 还是单调栈,现在有点感觉了,单调栈大概就是能求出当前值左右的比它大或小的数的范围.这题用高度作为单调栈,分别往左右找比当 ...
- [leetcode]84. Largest Rectangle in Histogram c语言
题目 Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- 84直方图最大矩形覆盖 · Largest Rectangle in Histogram
[抄题]: Given n non-negative integers representing the histogram's bar height where the width of each ...
- LeetCode84 Largest Rectangle in Histogram
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- LeetCode 321. 拼接最大数(单调栈)*
文章目录 1. 题目 2. 解题 1. 题目 给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字. 现在从这两个数组中选出 k (k <= m + n) ...
- LeetCode 901. 股票价格跨度(单调栈)
1. 题目 编写一个 StockSpanner 类,它收集某些股票的每日报价,并返回该股票当日价格的跨度. 今天股票价格的跨度被定义为股票价格小于或等于今天价格的最大连续日数(从今天开始往回数,包括今 ...
最新文章
- 【Hibernate步步为营】--双向关联一对一映射具体解释(一)
- springMVC - 之拦截器
- 黑马程序员pink老师前端入门教程,零基础必看的JavaScript基础语法视频教程(二)
- Mac平台下Opencv开发环境搭建
- MSSQL-最佳实践-如何监控备份还原进度
- 无法打开caffe.pb.h, no such file or directory错误
- Maven自学日志01---安装
- 【连载】Django入门到实战(一)
- 2021-09-02最小栈
- windows加linux双系统安装方法
- java dem数据格式解析_【转载】DEM数据的介绍,获取,处理
- 北京地区研究生学位英语(GET)词汇
- Android平台车牌识别开发手册
- 我的世界手机版服务器显示即将推出,我的世界1.11-pre1发布 正式版本官方即将推出...
- CentOS下MySQL安装失败,报socket '/tmp/mysql.sock错误解决方法
- Ingress暴露服务的方式
- MES系统业务需求收集及整理
- 最终幻想14 服务器维护,《FF14》服务器遭到攻击 官方补偿方案今日不收时长费...
- Web前端开发入门教程,HTML5+CSS3+JS教程,达到web前端工程师的水平
- 7 125 kHz RFID技术