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.每次弹栈时,都要计算最大的面积。
代码如下:
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
height.push_back();
stack<pair<int, int> > h;//height, index
int res = ;
for( int i = ; i < height.size() ; i++ )
{
pair<int, int> tmp;
if( h.size() == || h.top().first <= height[i])
{
h.push(make_pair(height[i], i));
}
else
{
while(h.size() > && h.top().first > height[i])
{
tmp = h.top();
h.pop();
res = max(res, tmp.first * (i-tmp.second));
}
h.push(make_pair(height[i], tmp.second));
}
}
height.pop_back();
return res;
}
};
leetcode Largest Rectangle in Histogram 单调栈的更多相关文章
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- LeetCode: Largest Rectangle in Histogram 解题报告
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- (数组)Largest Rectangle in Histogram(栈解问题)
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...
- [LeetCode] Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [LeetCode] Largest Rectangle in Histogram 解题思路
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [leetcode]Largest Rectangle in Histogram @ Python
原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...
随机推荐
- 补丁安装命令(WUSA)
wusa windows6.1-kb2716513-x64.msu /quiet /norestart msxml4-kb2758694-enu.exe /quiet /norestart 安装.ms ...
- 面试题总结之C/C++/MISC
C C pointer,指向数据结构与指向char的指针有区别吗 它们正做+1运算时产生的位移不同 分配在堆的内存与分配在堆栈的内存有什么不同 分配在堆的内存要手动去释放 C structure,数据 ...
- 聊聊 iOS 开发中的协议
前言 何为协议,简单来说在OC中我们使用关键字@protocol可以声明一个协议,并在协议中添加多个属性.方法供于遵循者实现,从某个角度上来说,这是一种不同于category机制的category.在 ...
- Java基础知识强化之网络编程笔记17:Android网络通信之 使用Http的Post方式读取网络数据(基于HTTP通信技术)
使用Http的Post方式与网络交互通信.Post方式需要向网络传输一部分数据,同时具有输入流和输出流. 详见:Android(java)学习笔记210:采用post请求提交数据到服务器(qq登录案例 ...
- 2014年6月5日 深圳 IBM 安全解决方案会议通知
2014年6月5日 深圳 IBM 安全解决方案会议通知 http://gdtesting.com/news.php?id=191 时间: 2014年6月5日 地点: 深圳大中华喜来登 议程: IBM安 ...
- SQL中N $ # @的作用
declare @sql nvarchar(4000) set @sql= N'select @TotalRecords=count(*) from ' + N'(' + @sqlFullPopula ...
- Elasticsearch template(待续...)
动态模板 Dynamic templates allow you to define custom mappings that can be applied to dynamically added ...
- oracle学习总结4
1:三范式a:一张表里必须要有主键,列不可分.b:如果一张表里面,两个字段作为主键,那么其他字段不能够部分依赖这两个字段. 2:pl sql:Procedural language(过程语言) 写一个 ...
- 关于egit的日常操作总结
$git fetch -p --prune -p -- remove any remote tracking branches that no longer exist remotely prune的 ...
- 关于MapReduce
MapReduce是Google提出的一个软件架构,用于大规模数据集(大于1TB)的并行运算.概念“Map(映射)”和“Reduce(归纳)”,及他们的主要思想,都是从函数式编程语言借来的,还有从矢量 ...