Largest Rectangle in Histogram
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的更多相关文章
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...
- 47. Largest Rectangle in Histogram && Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- 关于LeetCode的Largest Rectangle in Histogram的低级解法
在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
- 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 ...
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
- LeetCode: Largest Rectangle in Histogram 解题报告
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 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 ...
随机推荐
- Linq to xml 小例
static void Main(string[] args) { string strXml = @"<?xml version='1.0' en ...
- eclipse 高亮代码
本文整合自网络上的两种靠谱的使eclipse代码高亮的方式. 其实你可以在Window->proferences->java->editor->syndex coloring- ...
- AnyCAD.NET C#开发CAD软件实践(一)
免费的AnyCAD.NET发布了!俺喜欢的C#有了大展前途的机会了. 打算用这个框架搭建一套实用的CAD系统,目标是能买出去10套以上. 先看看AnyCAD.NET的自我介绍. http://www. ...
- SVN使用说明
一,安装客户端SVN 1.下载 "svn小乌龟"后,进行安装.如下图: 安装完成后,右键项目文件夹就可以看到如下: 2:checkout项目文件. 新建或者进入目录下(比如qian ...
- DLL 导出封装类
首先使用Wizard创建一个Win32 Dynamic-Link Library工程,然后定义一个简单的C++类CInDLL.由于该类会被工程之外的文件所引用,所以需要对这个类进行引出.因为只有引出后 ...
- 《我是一只IT小小鸟》读后感
过了半个学期的大学生活,说实话,我是迷茫的,因为我还没有足够的了解IT这门课程,也不知道怎么学好这门课程. 直到老师推荐我们读一本书<我是一只it小小鸟>,起初,我并不认为它是一本多么好的 ...
- iOS App 获取从后台返回前台时的页面
产品美美的给小伙伴提了一个需求,当程序从后台进入前台时,如果是指定的页面,则弹出提示框. 大家首先想到的方法就是通过 AppDelegate.h 进行控制,相对复杂的步骤就是 在程序进入后台时对当前页 ...
- Exploratory Undersampling for Class-Imbalance Learning
Abstract - Undersampling is a popular method in dealing with class-imbalance problems, which uses on ...
- bak骗子公司
李波 身份证:310101197510313215 手机:13916407777 18621624812 13916821206住址:上海QQ:87766938 沪EE5781 奥迪Q7李寻欢77 s ...
- HyperV采用硬盘拷贝的方式迁移虚拟机后的问题处理
公司有一台RSA认证服务器,是在windows 2008 R2下的虚拟机,最近总是出现服务中断的情况,考虑到宿主机性能较差,于是想迁移到新的服务器中. 本想通过SCVMM来迁移,但因功能不可用,所以采 ...