[LeetCode OJ] 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 height = [2,1,5,6,2,3],
return 10.
方法一:两层循环遍历,复杂度O(n2)
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int maxArea=;
for(unsigned i=; i<height.size(); i++)
{
int min = height[i];
for(unsigned j=i; j<height.size(); j++)
{
if(height[j]<min)
min = height[j];
int area = min*(j-i+);
if(area>maxArea)
maxArea = area;
}
}
return maxArea;
}
};
方法二:用堆栈保存重要位置,复杂度O(n)
class Solution {
public:
int largestRectangleArea(vector<int> &height) { //用堆栈来实现
stack<unsigned> st;
unsigned maxArea = ;
for(unsigned i=; i<height.size(); i++)
{
if(st.empty())
st.push(i);
else
{
while(!st.empty())
{
if(height[i]>=height[st.top()])
{
st.push(i);
break;
}
else
{
unsigned idx=st.top();
st.pop();
unsigned leftwidth = st.empty() ? idx : (idx-st.top()-);
unsigned rightwidth = i - idx-;
maxArea = max(maxArea, height[idx]*(leftwidth+rightwidth+));
}
}
if(st.empty())
st.push(i);
}
}
unsigned rightidx = height.size();
while(!st.empty())
{
unsigned idx = st.top();
st.pop();
unsigned leftwidth = st.empty() ? idx : (idx-st.top()-);
unsigned rightwidth = rightidx - idx-;
maxArea = max(maxArea, height[idx]*(leftwidth+rightwidth+));
}
return maxArea;
}
};
[LeetCode OJ] Largest Rectangle in Histogram的更多相关文章
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
- Java for LeetCode 084 Largest Rectangle in Histogram【HARD】
For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...
- 关于LeetCode的Largest Rectangle in Histogram的低级解法
在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...
- [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- leetCode 84.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浅析
首先上题目 Given n non-negative integers representing the histogram's bar height where the width of each ...
- [LeetCode#84]Largest Rectangle in Histogram
Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...
- LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形
原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...
随机推荐
- js的 new image()用法[转]
创建一个Image对象:var a=new Image(); 定义Image对象的src: a.src=”xxx.gif”; 这样做就相当于给浏览器缓存了一张图片. 图像对象: 建立图像对 ...
- Poj 3695-Rectangles 矩形切割
Rectangles Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3846 Accepted: 1124 Descri ...
- 使用aespython进行ECB加解密示例
CBC示例: from aespython import cbc_mode, key_expander, aes_cipher key = 'MluJMEZegVmrnWDaBiG1j7lqpkxNH ...
- Linux下常用I/O模型
Linux异步I/O是Linux内核中提供的一个相当新的增强.它是2.6版本内核的一个标准特性,异步非阻塞I/O背后的基本思想是允许进程发起很多I/O操作,而不用阻塞或等待任何操作完成.稍后或在接收到 ...
- Java SAX Schema Validation
It is possible to turn on XML Schema validation during parsing with a SAXParser. Here is how it look ...
- SOA技术的进化史
SOA 是一种程序设计思想,其实早在远古时代(计算机史)它就已经出现了.无非就是把系统分解,将数据和业务逻辑部分尽量独立出来,然后以服务形式提供给另外的系统共用. 那时也有一些可以实现 SOA 的工具 ...
- 手机终于能连接android studio
折腾了三天,终于能连上了,网上各种方法都试了,就是不行,结果安装了腾迅安全管家,自动安装了手机驱动就好了,原来一直都是驱动惹的祸啊......
- json中文编码问题
在和微信接口交互的过程中需要传参json而里面有中文的存在,所以json_encode($data)编码后会形成 {"group":{"name":" ...
- 准备在新项目中使用pgsql【资源收集】
pgsql大象数据库 是我最近在关注的一款开源数据库,可以自由修改,没那么多限制,准备在新项目中使用 postgresql中国下载站 http://www.postgres.cn/download#s ...
- xml 与 DataSet 互相转换
本文转载:http://www.cnblogs.com/30ErLi/archive/2010/09/21/1832694.html XmlDatasetConvert 该类提供了四种方法: 1.将x ...