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 ...
随机推荐
- 浅析pc机上如何将vmlinuz-2.6.31-14-generic解压出vmlinux
浅析pc机上如何将vmlinuz-2.6.31-14-generic解压出vmlinux luther@gliethttp:~$ vim /boot/grub/grub.cfg 可以看到我们进入的系统 ...
- hbctf---whatiscanary学习
题目中除了能栈溢出实在找不到其他能泄露信息的地方了.而且也没法修改GOT表,始终绕不过stack_chk_fail函数.感到无从下手.只到官方给WP了,才觉得自己基础太过浅薄了. 如果我们仔细观察崩溃 ...
- 多线程入门-第七章-线程的同步Synchronized
/* 异步编程模型:两个线程执行自己的,互不影响. 同步编程模型:t1和t2执行,t2必须等t1执行结束之后才能执行. 为什么要线程同步? 1.为了数据的安全,尽管应用程序的使用率降低,但是为了保证数 ...
- Mysql日常操作
创建用户并授权 grant all privileges on test.* to "test"@"localhost" identified by " ...
- MySQL的表分区详解 - 查看分区数据量,查看全库数据量----转http://blog.csdn.net/xj626852095/article/details/51245844
查看分区数据量,查看全库数据量 USE information_schema; SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PAR ...
- Linux内核中namespace之PID namespace
前面看了LInux PCI设备初始化,看得有点晕,就转手整理下之前写的笔记,同时休息一下!!~(@^_^@)~ 这片文章是之前写的,其中参考了某些大牛们的博客!! PID框架的设计 一个框架的设计会考 ...
- 感知器python
感知器学习的目标是求得一个能够将训练集正实例点和负实例点·完全正确分开的分离超平面.即找到这超平面的参数w,b. 超平面定义 w*x+b=0 其中w是参数,x是数据.公式很好理解以二维平面为例,w有两 ...
- 深入浅出MySQL-DDL语句
DDL语句 DDL是数据定义语言的缩写,简单来说,就是对数据库内部的对象进行创建.删除.修改等操作的语言.它和DML(数据操纵语言)的最大区别是DML知识对表内部的数据操作,而不涉及表的定义.结构的修 ...
- Boinformatics-2018-10-1-目录
1.基因分析 --Using standard microbiome reference groups to simplify beta-diversity analyses and facilita ...
- Java将数据写进excel
Java将数据写进excel Java将数据写进excel class User { private String name ; private String password; public Use ...