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 ...
随机推荐
- Vim 字符集问题
使用CentOS中的Vim 文本编辑器出现中文乱码的问题. 凡是字符乱码的问题,都是字符集不匹配的问题引起的.这里的字符集不匹配只的是文件的编码和解码方式不匹配,同时可能涉及到不只一次的解码过程. ...
- Girls' research---hdu3294(回文子串manacher)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3294 给出一个字符串和加密的字符规律 例如 c abcba c代表把串中的c改成a,d改成b... b ...
- Flask之视图函数
视图示例 @app.route('/hello') def hello(): return 'Hello World' if __name__ == '__main__': app.run() 特殊的 ...
- 2.AS入门教程
AndroidStudio 本文是关于androidStudio的一些基础知识 介绍 Google官方的Android集成开发环境(IDE = Integrated Development Envir ...
- docker+MySQL+读写分离
一.拉取mysql镜像文件docker pull mysql二.查看镜像docker images三.创建配置文件目录mkdir /data/docker/mysql/{master,slave} - ...
- Go实现查找目录下(包括子目录)替换文件内容
[功能] 按指定的目录查找出文件,如果有子目录,子目录也将进行搜索,将其中的文件内容进行替换. [缺陷] 1. 没有过滤出文本文件 2. 当文件过大时,效率不高 [代码] package main i ...
- oracle 查看隐藏参数
隐藏参数 (hidden parameters) ,由oracle内部使用,以 '_' 开头. 可以通过以下两种方式查看所有隐藏参数: SELECT i.ksppinm name, i.ksppd ...
- selenium 模块
介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如 ...
- 爬虫——请求库之requests
阅读目录 一 介绍 二 基于GET请求 三 基于POST请求 四 响应Response 五 高级用法 一 介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,reque ...
- SQL Server 使用 Hierarchyid 操作层次结构数据
层次结构数据定义为一组通过层次结构关系互相关联的数据项. 在层次结构关系中,一个数据项是另一个项的父级或子级. sql server2008开始内置的 hierarchyid 数据类型使存储和查询层次 ...