【LeetCode】84. Largest Rectangle in Histogram
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
.
具体思路也是用stack的思想,相比其他的需要40ms以上我使用了将while循环拆分开来,从而可以将
算法的复杂度更进一步降低,运算时间为16ms
class Solution {
public:
int largestRectangleArea(vector<int>& height) {
if(0==height.size())
return 0;
if(1==height.size())
return height[0];
vector<int>index;
height.push_back(-1);
int count=-1;
int ans=0;
int len=height.size();
for(int i=0;i<height.size();)
{
int peak=index.size()-1;
if(-1==peak)
{
index.push_back(i);
i++;
}
while(i<len&&height[i]>=height[index[peak]])
{
index.push_back(i);
i++;
peak=index.size()-1;
}
peak=index.size()-1;
int tmp=index[peak];
while(peak>-1&&height[index[peak]]>height[i])
{
if(peak==0)
{
count=height[index[peak]]*i;
index.pop_back();
peak=index.size()-1;
}
else
{
int mm=index[peak];
index.pop_back();
peak=index.size()-1;
count=height[mm]*(i-index[peak]-1);
}
if(count>ans)
ans=count;
}
}
return ans;
}
};
【LeetCode】84. Largest Rectangle in Histogram的更多相关文章
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- 【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 85. Maximal Rectangle
问题描述: 84:直方图最大面积. 85:0,1矩阵最大全1子矩阵面积. 问题分析: 对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPo ...
- 【一天一道LeetCode】#84. Largest Rectangle in Histogram
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- 【LeetCode】084. Largest Rectangle in Histogram
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- LeetCode OJ 84. Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 【Lintcode】122.Largest Rectangle in Histogram
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 【LeetCode】85. Maximal Rectangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...
随机推荐
- 在mui中遇到的内容覆盖导航栏的问题
一.问题描述: 公司项目中为了让内容以页面的形式显示,并要格式化页面内容,采用了百度的UEditor编辑器来显示内容,但是遇到了一个问题就是当下拉页面到一定距离之后,页面上方的导航栏会被内容遮盖. 二 ...
- 利用Boost影响Lucene查询结果的排序
转自:http://catastiger.iteye.com/blog/803796 前提:不对结果做sort操作. 在搜索中,并不是所有的Document和Fields都是平等的.有些技术会要 ...
- 【Struts2学习笔记-3】常量配置
Struts2常量 配置Struts2常量值有3个地方,1)在struts.properties文件中配置常量:2)在web.xml文件中配置FileterDispatcher指定初始化参数来配置常量 ...
- Spring实战6:利用Spring和JDBC访问数据库
主要内容 定义Spring的数据访问支持 配置数据库资源 使用Spring提供的JDBC模板 写在前面:经过上一篇文章的学习,我们掌握了如何写web应用的控制器层,不过由于只定义了SpitterRep ...
- MySQL压缩包安装
1.解压缩 2.添加环境变量 3.添加配置文件 my.ini 4.以管理员身份初始化数据库 mysqld --initialize --user=mysql --console 5.以管理员身份将My ...
- 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- memcached简介(转)
背景 memcached是一个高性能.分布式的内存对象缓存系统. memcached广泛应用在大负载高并发的网站上,是一种非常成熟的产品(称为一项技术也未尝不可).像facebook,yout ...
- Spark 1.4连接mysql诡异的问题及解决
在spark-default.conf文件中明明配置了mysql的数据源连接 随后启动spark-shell 执行如下测试代码: import org.apache.spark.{SparkConte ...
- Going Home (hdu 1533 最小费用流)
集训的图论都快结束了,我才看懂了最小费用流,惭愧啊. = = 但是今天机械键盘到了,有弄好了自行车,好高兴\(^o^)/~ 其实也不是看懂,就会套个模板而已.... 这题最重要的就是一个: 多组输入一 ...
- SharedPreferences实现自动登录记住用户名密码
最近Android项目需要一个自动登录功能,完成之后,特总结一下,此功能依靠SharedPreferences进行实现. SharedPreferences简介 SharedPreferences ...