又是一道构思巧妙的题,暴力求解复杂度太高,通过构造一个递增栈来解决:如果当前元素小于栈顶元素,则说明栈内已经构成一个

递增栈,则分别计算以每个元素为最低值的面积;反之,则入栈。

int largestRect(vector<int> &height)
{
stack<int> s;//定义一个单调递增栈
height.push_back();//定义单调递增栈的最后一个
int result = ;//记录当前最大的面积
for (int i = ; i < height.size();)//满足条件i才递增
{
if (s.empty() || height[i]>s.top())
{
s.push(i++);//只有当前元素大于栈顶元素是才入栈相应的序号,构造递增栈
}
else {
//当前元素小于栈顶元素时
int tmp = s.top();//保留栈顶元素
s.pop();//弹出直至当前元素大于栈顶元素,使栈仍然是递增的
//
result = max(result, height[tmp]*(s.empty() ? i : i - s.top() - ));
}
} return result;
}

注:上述代码有误,应该是height[s.top()]

Leetcode 之Largest Rectangle in Histogram(40)的更多相关文章

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

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

  2. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

  3. Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

    For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...

  4. 关于LeetCode的Largest Rectangle in Histogram的低级解法

    在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...

  5. [LeetCode] 84. 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浅析

    首先上题目 Given n non-negative integers representing the histogram's bar height where the width of each ...

  7. [LeetCode OJ] Largest Rectangle in Histogram

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

  8. [LeetCode#84]Largest Rectangle in Histogram

    Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...

  9. LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形

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

  10. [leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形

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

随机推荐

  1. BZOJ3140:[HNOI2013]消毒——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3140 https://www.luogu.org/problemnew/show/P3231 最近在 ...

  2. 【转】TCP拥塞控制,慢启动、拥塞避免、快重传以及快恢复

    转自:http://blog.csdn.net/yusiguyuan/article/details/22847787 注:本文绝大部分是来自转载的博客,还补充了少量内容. 一.TCP的拥塞控制 拥塞 ...

  3. The driver has not received any packets from the server

    解决方法: jdbc的url添加参数: jdbc.url=jdbc:mysql://localhost:3306/totosea?useUnicode=true&characterEncodi ...

  4. bzoj3524: [Poi2014]Couriers(主席树)

    主席树(可持久化权值线段树)初探... 修改一个点只对树上logn个点有影响,所以新建logn个点就行了,总共新建mlogn个点. 查询一个区间[l,r],相当于将数一个一个加进树,询问第l到第r次操 ...

  5. Mysql数据库的主从复制

    怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作:  1.1.版本一致  1.2.初始化表,并在后台启动mysql  1.3.修改root的密码 2.修 ...

  6. eclipse ide for java ee developers 开发环境搭建(J2EE) 【转载】

    使用eclipse真的有年头了,相信java程序员没有不知道它的,最近在给团队中新来的应届生做指导,专门讲解了一下Eclipse开发环境的搭建过程, 一是帮助他们尽快的熟悉IDE的使用,二也是保证团队 ...

  7. HDU 1715 大数java

    大菲波数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. Android IntentService分析

    IntentService其实是一个很通用的知识点,最近看了下阿里巴巴Android开发手册,再次记录下 阿里巴巴Android开发手册 [强制]避免在 BroadcastReceiver#onRec ...

  9. bzoj 2457 [BeiJing2011]双端队列 模拟+贪心

    [BeiJing2011]双端队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 457  Solved: 203[Submit][Status][D ...

  10. eclipse ----- indexer

    使能indexer,可以实现变量.函数等的跳转, 即跳转到定义的位置