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的更多相关文章

  1. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  2. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  3. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  4. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  5. [leetcode]Largest Rectangle in Histogram @ Python

    原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...

  6. [LeetCode] Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  7. 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 ...

  8. [LeetCode] Largest Rectangle in Histogram 解题思路

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  9. LeetCode: Largest Rectangle in Histogram 解题报告

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  10. LeetCode——Largest Rectangle in Histogram

    Question Given n non-negative integers representing the histogram's bar height where the width of ea ...

随机推荐

  1. 2017 Multi-University Training Contest - Team 1—HDU6044

    Limited Permutation 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6044 题意:现在有一个排列p1,p2,p3,p4,p5,p6… ...

  2. 哈密顿绕行世界问题---hdu2181(全排列问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 题意很容易理解,dfs就可以了 #include <iostream> #inclu ...

  3. mysql删除重复数据,保留最新的那一条

    因为数据库没键外键,在关联查询的时候,会碰到查询条数多余数据库实际条数,这因为关联字段在表中有重复值而导致的. 解决方案: 1.数据库脚本删除重复数据,保留最新的一条 2.对关联字段增加唯一约束 例如 ...

  4. jQuery -&gt; 使用andSelf()来包括之前的选择集

    版权声明:本文为博主原创文章.转载请注明出处 https://blog.csdn.net/FeeLang/article/details/26254793 当我们使用Destructive Metho ...

  5. Shiro起步

    1.测试环境  IntelliJ Idea 2.pom配置 <?xml version="1.0" encoding="UTF-8"?> <p ...

  6. HDU2425:Hiking Trip(简单bfs,优先队列实现)

    题目: 传送门 题意很简单就不解释了,水题一道. #include <iostream> #include <string.h> #include <stdio.h> ...

  7. HALCON里面的一维测量。

    第一步:将图片导入, 拿到图片的名字 和窗口的句柄 第二步:创建一个测量区域.这个测量区域是一个矩形,假设他的名字叫A gen_measure_rectangle2 (TmpCtrl_Row,//输入 ...

  8. centos7命令2

    都代表一个程序find / -name '*name*' 查找根目录下的包含name的文件名 权限不足的话在命令前面加上sudo find / -size 1G 查找根目录下文件大小为1G的文件 fi ...

  9. Python3 安装第三方包

    打开cmd(切记是cmd,不是Python3.6那个敲代码环境)输入 pip3 install numpy 即可(安装numpy包)

  10. ionic 配置打包环境

    配置java环境就不说了,太简单 下载AndroidSdkAndroid SDK Tools翻过墙的朋友可以去Google Android的官网上下载:http://developer.android ...