作者: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. 免费开放的API

    1)新浪IP地址查询API接口 http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=IP地址 format有 js. ...

  2. Chart控件的多种使用方法

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

  3. POJ_1365_Prime_Land

    //懒得解释 #include <iostream> #include <cstring> #include <cmath> #include <cstdio ...

  4. TQ210裸机编程(2)——LED流水灯

    两个文件start.S和led.c start.S .global _start                @声明一个全局的标号 _start:     bl main               ...

  5. qt-vs-addin-版本支持

    qt-vs-addin-1.2.0-opensource.exe         VS200X qt-vs-addin-1.2.1-opensource.exe         VS200X qt-v ...

  6. 如何获取SQL Server数据库元数据的方法

    发布时间:2007.06.15 05:05    来源:赛迪网    作者:3946469 元数据简介 元数据 (metadata) 最常见的定义为“有关数据的结构数据”,或者再简单一点就是“关于数据 ...

  7. GWT 中日期格式化 ,处置Date

    GWT的view中不能用java原生的DateFormat 必须使用gwt封装的格式化方法,方法如下 import com.google.gwt.i18n.client.DateTimeFormat; ...

  8. Intent实现页面跳转

    Intent实现页面跳转: 1. startActivity(intent) 2. startActivityForResult(intent,requestCode); onActivityResu ...

  9. 可扩展的listview--Expandablelistview

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  10. posix thread线程

    1. pthread线程通过调用你提供的某些函数开始.这个“线程函数”应该只有一个void*型参数,并返回系统的类型.2. 通过向pthread_create函数传递线程函数的地址和线程函数调用的参数 ...