【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- ...
随机推荐
- VS2012远程调试(winform+web 远程调试)
VS2012远程调试 一.调试WinFrom 程序 安装rtools_setup_x64 下载 配置Remote 启动Remote debugger 默认端口4016,选择工具-〉选项,选择 无身 ...
- Python基础教程【读书笔记】 - 2016/7/24
希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第九波:第9章 魔法方法.属性和迭代器 在Python中,有的名称会在前面和后面都加上两个下划线,这种写法很特别.已 ...
- eclipse中tomcat发布失败(Could not delete May be locked by another process)原因及解决办法
在eclipse中tomcat发布项目时,偶尔出现了以下情况: publishing to tomcat v7.0 services at localhost has encountered a pr ...
- 重复ID的记录,只显示其中1条
--重复ID的记录,只显示其中1条 --生成原始表 select * into #tempTable from ( select '1' as id ,'a' as name union all se ...
- ajax实现--技术细节详解
ajax原理和XmlHttpRequest对象 Ajax的原理简单来说通过XmlHttpRequest对象来向服务器发异步请求,从服务器获得数据,然后用javascript来操作DOM而更新页面.这其 ...
- 黄聪:MYSQL提交一批ID,查询数据库中不存在的ID并返回
假设你数据库有个A表: ID NAME 1 aaa 2 bbb 3 ccc 4 ddd 需求:给你几个ID,返回A表中不存在的ID? 例如提交1,2,8,9 返回8,9 sel ...
- python(14)类,方法,对象,实例
类: 简单来说就是一个函数的集合,在这个集合里面你定义了很多个函数: 方法:其实就是你定义的这些函数. 对象:简单来说就是自身具有多个属性(也可以说是子变量)的变量而已. 在下面的例子中class P ...
- Spring中IOC和AOP的详细解释
我们是在使用Spring框架的过程中,其实就是为了使用IOC,依赖注入,和AOP,面向切面编程,这两个是Spring的灵魂. 主要用到的设计模式有工厂模式和代理模式. IOC就是典型的工厂模式,通过s ...
- iOS获取电量方法
ios简单的方法: [UIDevice currentDevice].batteryMonitoringEnabled = YES; double deviceLevel = [UIDevice cu ...
- ios 开发,通讯录信息调用常用方法,这个比较全,不用再整理了
ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfA ...