LeetCode——largest-rectangle-in-histogram1
Question
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

Above is a histogram where width of each bar is 1, given height =[2,1,5,6,2,3].

The largest rectangle is shown in the shaded area, which has area =10unit.
For example,
Given height =[2,1,5,6,2,3],
return10.
Code
/*
用堆栈计算每一块板能延伸到的左右边界
对每一块板
堆栈顶更矮,这一块左边界确定,入栈
堆栈顶更高,堆栈顶右边界确定,出栈,计算面积
入栈时左边界确定
出栈时右边界确定
堆栈里元素是递增的
本质:中间的短板没有用!
复杂度 O(n)
*/
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
stack<int> tb;
int res = 0;
for (int i = 0; i < height.size(); i++) {
while (!tb.empty() && height[tb.top()] >= height[i]) {
int index = tb.top();
tb.pop();
if (tb.empty())
// 当前长度的最矮高度
res = max(i * height[index], res);
else {
// 底 * 高
res = max((i - tb.top() - 1) * height[index], res);
}
}
tb.push(i);
}
// 这里的n相当于相面的i遍历到height.size();
// 所以用n来计算底的长度
int n = height.size();
while (!tb.empty()) {
int index = tb.top();
tb.pop();
if (tb.empty())
// 最矮的列
res = max(n * height[index], res);
else {
res = max((n - tb.top() - 1) * height[index], res);
}
}
return res;
}
};
LeetCode——largest-rectangle-in-histogram1的更多相关文章
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- [LeetCode] 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(直方图最大面积)
http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...
- [leetcode]Largest Rectangle in Histogram @ Python
原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...
- [LeetCode] 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 TODO O(N)
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 bar is ...
- LeetCode: Largest Rectangle in Histogram 解题报告
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- LeetCode——Largest Rectangle in Histogram
Question Given n non-negative integers representing the histogram's bar height where the width of ea ...
随机推荐
- packages managers
nodejs npm/bower/component ...rubygemsperl cpanpython pipOS X homebrewsublime text的package-control 那 ...
- PHP 错误日志
display_errors 错误回显,一般常用语开发模式,但是很多应用在正式环境中也忘记了关闭此选项.错误回显可以暴露出非常多的敏感信息,为攻击者下一步攻击提供便利.推荐关闭此选项. display ...
- 【react表格组件】material-table 基本用法 & 组件override
教程: https://mbrn.github.io/material-table/#/ https://material-ui.com/api/table/ github: https://gith ...
- vSphere虚拟机磁盘热扩容
1.添加硬盘 2.刷新服务器文件系统 新添加的硬盘需要刷新文件系统,要不然不能识别新添加的硬盘. 对scsi_host进行重新扫描,查找 scsi 驱动器的号 驱动号为scsi后面的数字,即为2,此时 ...
- 一次因为文件名开头包含空格而导致FTP文件一直无法下载的悲剧!
最近负责公司研究新的多渠道打包方案,之前的打包方案太慢了,因此采用了美团的Android Signature V2 Scheme签名下的新一代渠道包打包神器 方案进行了多渠道打包.但是由于马虎,在配置 ...
- Mybatis框架学习总结-调用存储过程
设计需求 查询数据库,查询得到男性或女性的数量,如果传入的参数是0查询女性,否则查询男性. 准备数据库表和存储过程 1.准备person表: CREATE TABLE person( id INT P ...
- HTML5开源RPG游戏引擎lufylegendRPG 0.1发布
一,小小开篇 首先不得不先介绍一下这个引擎: lufylegendRPG是lufylegend的拓展引擎,使用它时,需要引入lufylegend.同时您也需要了解lufylegend语法,这样才能 ...
- 机器学习算法(优化)之一:梯度下降算法、随机梯度下降(应用于线性回归、Logistic回归等等)
本文介绍了机器学习中基本的优化算法—梯度下降算法和随机梯度下降算法,以及实际应用到线性回归.Logistic回归.矩阵分解推荐算法等ML中. 梯度下降算法基本公式 常见的符号说明和损失函数 X :所有 ...
- 查看电脑已经连过的wifi密码
用下面两条命令可以完成 查看当前系统已经保存的网络 netsh wlan show profiles 查看wifi指定密码 netsh wlan show profiles name="wi ...
- 《mysql必知必会》读书笔记--游标的使用
游标的使用 MySQL中游标只能用于存储过程 创建游标 CREATE PROCEDURE processorders() BEGIN DECLARE ordernumbers CURSOR FOR S ...