Leetcode 之Largest Rectangle in Histogram(40)

又是一道构思巧妙的题,暴力求解复杂度太高,通过构造一个递增栈来解决:如果当前元素小于栈顶元素,则说明栈内已经构成一个
递增栈,则分别计算以每个元素为最低值的面积;反之,则入栈。
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)的更多相关文章
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
- 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 ...
- 关于LeetCode的Largest Rectangle in Histogram的低级解法
在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...
- [LeetCode] 84. 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 ...
- [LeetCode OJ] Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [LeetCode#84]Largest Rectangle in Histogram
Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...
- LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形
原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- [leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
随机推荐
- [bzoj4398] 福慧双修 最短路 二进制分组
---题面--- 题解: 考场上看的这道题,,,当时70分算法打挂了,今天才知道这个也是原题.... 首先,对于不跟1相邻的边,肯定不会经过两次,因为经过两次就回来了,除了增加路径长度之外没有任何意义 ...
- LOJ6368:请让本题永远沉睡于此——题解
https://loj.ac/problem/6368 给一个分数,求对p=1e9+7取模的值. 给高一同学出的毒瘤模拟题,正好试试给loj传题,竟然过审了,虽然个人觉得很水,但是考试情况来看仅有一人 ...
- 探讨JavaScript的事件冒泡
JavaSciprt事件中有两个很重要的特性:事件冒泡以及目标元素. 当一个元素上的事件被触发的时候,比如说鼠标点击了一个按钮,同样的事件将会在那个元素的所有祖先元素中被触发.这一过程被称为事件冒泡: ...
- 关于springmvc下服务器文件打包成zip格式下载功能
关于springmvc下服务器文件打包成zip格式下载功能 2016年09月21日 11:22:14 toxic_guantou 阅读数:5731更多 个人分类: 技术点存储 版权声明:本文为博主 ...
- Python图像处理库(PIL)
官方:(详细)http://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html http://pillow.readthedocs.io/e ...
- 为什么使用centos部署服务器
这个是实验室同学面试的时候,面试官问的一个问题? 为什么选择centos系统,为什么centos系统用的比较多呢? 首先我们说下redhat红帽公司,它是全球最大的linux服务提供商,它的服务是最好 ...
- 解决oracle数据库 ora-00054:resource busy and acquire with NOWAIT specified 错误
解决oracle数据库 ora-00054:resource busy and acquire with NOWAIT specified 错误 本人在使用pl/sql developer 客户端调用 ...
- 更改本地hosts文件
在 C:\Windows\System32\drivers\etc 下更改 hosts 文件 127.0.0.1 www.baidu.com 这样访问 www.baidu.com 这个地址,其实是访问 ...
- Linux Shell下执行sqlplus
转载自: http://www.cnblogs.com/include/archive/2011/12/30/2307889.html 以下方法解决了在linux下自动的删除创建用户 sqlplus ...
- CentOS 5.8 安装python 和 yum
centos 5.8 资源路径: http://vault.centos.org/5.8/os/x86_64/CentOS/ rpm -Uvh http://vault.centos.org/5.8 ...