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 ...
随机推荐
- 第一次BC
BestCoder Round #90 1001 Kblack loves flag 太弱只写了这一道水题. 首先这个题面就是,完全不知道它在说什么.开始5mins后我还完全不知道这个题想要表达什么. ...
- Linux 守护进程创建原理及简易方法
1:什么是Linux下的守护进程 Linux daemon是运行于后台常驻内存的一种特殊进程,周期性的执行或者等待trigger执行某个任务,与用户交互断开,独立于控制终端.一个守护进程的父进程是in ...
- HDU2732 最大流
Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- IAR ------ 扩展关键字__weak
__weak作用:允许多个同名函数同时存在,但是最多只有一个没有__weak修饰.如果有non-weak函数(没__weak修饰),则此函数被使用,否则从__weak修饰的函数中选择其中一个. 下图来 ...
- java入门实现转换
设计思想 首先不用多说就是建立最基础的java创建,然后抛出一个异常处理来替我们检测用户的输入,这一点十分重要.然后就要进行输入工作,不必多说,网上的教程有一个Scanner的输入方法,我们引入一下. ...
- [LeetCode] 9. Palindrome Number ☆
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- 任何用户密码都能以sysdba角色登入
这是因为在安装Oracle的时候默认是使用了操作系统验证: 数据库用sysdba登录的验证有两种方式,一种是通过os认证,一种是通过密码文件验证:登录方式有两种,一种是在数据库主机直接登录(用os认证 ...
- 深度解析Java多线程的内存模型
内部java内存模型 硬件层面的内存模型 Java内存模型和硬件内存模型的联系 共享对象的可见性 资源竞速 Java内存模型很好的说明了JVM是如何在内存里工作的,JVM可以理解为java执行的一个操 ...
- 【设计模式】 模式PK:命令模式VS策略模式
1.概述 命令模式和策略模式的类图确实很相似,只是命令模式多了一个接收者(Receiver)角色.它们虽然同为行为类模式,但是两者的区别还是很明显的.策略模式的意图是封装算法,它认为“算法”已经是一个 ...
- 在vsagent上运行.dll录制文件。
https://msdn.microsoft.com/en-us/library/ms182487.aspx 1. cd C:\Program Files (x86)\Microsoft Visual ...