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 = 10 unit.

For example,
Given heights = [2,1,5,6,2,3],
return 10.

class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
heights.push_back();
stack<int> s;
int res = ;
int i = ;
while(i < heights.size()){
if(s.empty() || heights[i] > heights[s.top()]){
s.push(i);
i++;
}else{
int cur = s.top();
s.pop();
if(s.empty()){
res = max(res,heights[cur]*i);
}else{
res = max(res,heights[cur]*(i-s.top()-));
}
}
}
return res; }
};

Largest Rectangle in Histogram的更多相关文章

  1. LeetCode 笔记系列 17 Largest Rectangle in Histogram

    题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...

  2. 47. Largest Rectangle in Histogram && Maximal Rectangle

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

  3. 【LeetCode】84. Largest Rectangle in Histogram

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

  4. leetcode Largest Rectangle in Histogram 单调栈

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

  5. 关于LeetCode的Largest Rectangle in Histogram的低级解法

    在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...

  6. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

  7. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  8. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

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

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

  10. 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形

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

随机推荐

  1. IOS9 Swift

    Swift 2.0发布:即将开源,支持Linux [日期:2015-06-09] 来源:infoq  作者:Linux [字体:大 中 小]     在6月9日凌晨举行的WWDC 2015全球开发者大 ...

  2. iOS检测版本更新

    有时候为了需求,我们需要检测app版本更新今天在这里整合下 //获取当前版本号 NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDi ...

  3. 洛谷 1004 dp或最大费用流

    思路: dp方法: 设dp[i][j][k][l]为两条没有交叉的路径分别走到(i,j)和(k,l)处最大价值. 则转移方程为 dp[i][j][k][l]=max(dp[i-1][j][k-1][l ...

  4. isMobile

    var isMobile = { Android: function() { return navigator.userAgent.match(/Android/i); }, BlackBerry: ...

  5. Tomcat配置错误导致Quartz执行两次问题

    以下基于tomcat服务器 我们通常将域名映射到指定服务器的端口上,以通过域名直接访问服务,如http://www.abc.com域名已绑定到本机的80端口,项目名wechat,则直接访问http:/ ...

  6. web前端基础篇⑩

    1.960栅格式布局法屏幕分辨率为1024*768.采用接 main宽为960px的布局方式12列式:每格60px 间距20px 3 6 3版 三格式布局(最常用)16列式:每格40px 间距20px ...

  7. X3850M2安装CertOS 7 KVM 2--DMMP

    1,在DS8000中调整vg为单台服务器.检查另一台服务器内已经没有磁盘信息. 2,在余下的服务器中安装DMMP. 参考:http://edwin-wang.com/2012/08/device-ma ...

  8. Enterprise Architect 学习 之 用例图

    用例模型 用例模型用来记录系统的需求,它提供系统与用户及其他参与者的一种通信手段. 执行者 用例图显示了系统和系统外实体之间的交互.这些实体被引用为执行者.执行者代表角色,可以包括:用户,外部硬件和其 ...

  9. UpdatePanel里的Repeater和DropDownList

    在updatepanel里使用dropdownlist的AutoPostBack,正常情况下都可以局部刷新. 但是,如果updatepanel下是Repeater,repeater里绑定dropdow ...

  10. html学习心得

    注释:浏览器会自动地在段落的前后添加空行.(<p> 是块级元素) 提示:使用空的段落标记 <p></p> 去插入一个空行是个坏习惯.用 <br /> 标 ...