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 ...
随机推荐
- elasticsearch的store属性 vs _source字段
众所周知_source字段存储的是索引的原始内容,那store属性的设置是为何呢?es为什么要把store的默认取值设置为no?设置为yes是否是重复的存储呢? 我们将一个field的值写入es中,要 ...
- iwconfig
解决办法:清空/var/lib/dhclient/dhclient.leases文件里的所有内容 # sudo dhclient -r //release ip 释放IP # sudo dhclien ...
- Python爬虫实例(一)爬取百度贴吧帖子中的图片
程序功能说明:爬取百度贴吧帖子中的图片,用户输入贴吧名称和要爬取的起始和终止页数即可进行爬取. 思路分析: 一.指定贴吧url的获取 例如我们进入秦时明月吧,提取并分析其有效url如下 http:// ...
- Java技术大牛需要学习的25个技能
你需要精通面向对象分析与设计(OOA/OOD).涉及模式(GOF,J2EEDP)以及综合模式.你应该了解UML,尤其是class.object.interaction以及statediagrams. ...
- Count the string---hdu3336(kmp Next数组的运用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题意就是求串s的前缀的个数和: 例如:abab 前缀 个数 a 2 ab 2 ab ...
- python中读取json文件报错,TypeError:the Json object must be str, bytes or bytearray,not ‘TextIOWrapper’
利用python中的json读取json文件时,因为错误使用了相应的方法导致报错:TypeError:the Json object must be str, bytes or bytearray,n ...
- MapReduce学习笔记
一.MapReduce概述 MapReduce 是 Hadoop 的核心组成, 是专用于进行数据计算的,是一种分布式计算模型.由Google提出,主要用于搜索领域,解决海量数据的计算问题. MapRe ...
- Incorrect key file for table ' '; try to repair it
场景:为有150W的数据表增加字段时,报错 解决:在my.ini配置临时目录configure tmpdir. Where MySQL Stores Temporary Files
- Linux chmod命令
chmod用于管理文件或目录的权限,文件或目录权限的控制分别以读取(r).写入(w).执行(x)3种 可读可写可执行,抽象的用二进制来表示 1 代表拥有该权限,0 代表没有该权限,这样我们就可以看到 ...
- linux的虚拟机时间和物理机时间同步
1.查看和设置物理机硬件时间 查看硬件时间# hwclock --show或# clock --show 设置硬件时间# hwclock --set --date=”01/14/19 10:19″(月 ...