作者: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 单调栈的更多相关文章

  1. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  2. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  3. LeetCode: Largest Rectangle in Histogram 解题报告

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  4. (数组)Largest Rectangle in Histogram(栈解问题)

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  5. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  7. [LeetCode] Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  8. [LeetCode] Largest Rectangle in Histogram 解题思路

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  9. [leetcode]Largest Rectangle in Histogram @ Python

    原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...

随机推荐

  1. How to add “Maven Managed Dependencies” library in build path eclipse

    If you have m2e installed and the project already is a maven project but the maven dependencies are ...

  2. HDU 5289 Assignment(2015 多校第一场二分 + RMQ)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  3. 【10】令operator=返回一个reference to *this

    1.令operator= 返回一个reference to *this,为什么? 这只是一个协议,并无强制性.但是,为了与基本类型的行为保持一致性,强烈建议这么做.设计class 有一个宝典:一旦有疑 ...

  4. C#_delegate - 调用列表 计算阶乘

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Dele ...

  5. 安装opencms时遇到问题及解决方法

    1. MySQL system variable 'max_allowed_packet' http://blog.csdn.net/hqa_ii/article/details/6872367 安装 ...

  6. Google实习面试归来

    咱们寝室共有两个人收到面试通知,我和另一哥们G.      今天早上8:30起了个大早,洗漱完毕,简历复印完毕,就和G骑车到达了世贸中   心酒店那儿.真不愧是世贸中心啊,装修就是华丽,连看门的都是印 ...

  7. OC中-方法到底是如何使用的?

    方法:方法是Objective-C独有的一种结构,只能在Objective-C中声明.定义和使用,C语言不能声明.定义和使用. 1.类方法以+号开头,对象方法以-号开头+ (void) init;  ...

  8. C语言结构体的引入

    #include <stdio.h> struct student{ int ID; ]; int age; }; int main(){ //赋值: , }; ,.name=}; , , ...

  9. 用grunt搭建自动化的web前端开发环境-完整教程

    原稿:http://www.cnblogs.com/wangfupeng1988/p/4561993.html#!comments jQuery在使用grunt,bootstrap在使用grunt,百 ...

  10. 关于Eclipse中开发插件(二)

    原plugin.xml文件各个设置项的说明: 附上生成的文件代码: <?xml version="1.0" encoding="UTF-8"?> & ...