作者: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. Chart控件的多种使用方法

    花了近一周时间专门研究.net 3.5平台提供的Chart控件的使用方法,感觉该控件的功能很强大,做出的图表效果也很美观,使用方法也并不复杂.如今先讲下Chart控件的部署及一些基本使用方法. 一.安 ...

  2. javax.naming.NameNotFoundException:Name[ XXX] is not bound in this context.

    在用局部数据源去连数据库的时候,在本地的项目中,都是可以的,可是一部署到服务器上,就报错了. 报的错误是:javax.naming.NameNotFoundException:Name[ XXX] i ...

  3. LeetCode144:Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

  4. oracle之单行函数

     单行函数 ①.字符函数 LOWER(x):将x中的每一个单词都转换成小写 UPPER(x):将x中的每一个单词都转换成大写 INITCAP(x): 将x中的每一个单词的首字母转换成大写 CONC ...

  5. 在Struts2中使用ValueStack、ActionContext、ServletContext、request、session等 .

    笔者不知道该用哪个词来形容ValueStack.ActionContext等可以在Struts2中用来存放数据的类.这些类使用的范围不同,得到的方法也不同,下面就来一一介绍. 声明:本文参考Strut ...

  6. 编写程序,查找并删除forward_list<int>中的奇数元素

    #include<iostream> #include<forward_list> using namespace std; int main() { forward_list ...

  7. Ruby简介,附带示例程序

    Ruby语言是日本人松本行弘于1993年器开始着手研发,经历2年时间,发布了Ruby语言的第一个版本:0.95版.     Ruby是一种非常简介的解释性语言,一种纯粹的面向对象编程语言,甚至比Jav ...

  8. Google, FaceBook, Amazon 加州求职记 (转)

    http://blog.csdn.net/ithomer/article/details/8774006 http://www.myvisajobs.com 一年多前,出于显而易见的原因,下定决心肉身 ...

  9. Ⅰ.Spring的点点滴滴--序章

    spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架 .net篇(环境为vs2012+Spring.Core.dll) 新建一个控制台 using Spring.Context; ...

  10. c/c++中动态内存分配处理字符串的细节问题

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h&g ...