【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- ...
随机推荐
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
转自:http://blog.csdn.net/kalision/article/details/7692796 这两个类使用起来非常方便,可以完成我们对定时器的绝大多数需求 Timer类是用来执行任 ...
- Sublime Text 3 自定义配置快捷键
Settings-User: { "font_face": "Courier New", "font_size": 14.0, " ...
- 兼容所有浏览器---无缝上下左右交叉运动----原生js+css
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- solr环境搭建
介绍摘自百度百科:Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过 ...
- python(22)总结下最近遇到的编码问题
最近爬取,或者解析网页是总是遇到编码问题(我的版本:python2.7) 一.常见异常:UnicodeEncodeError: 'ascii' codec can't encode character ...
- 查看linux的版本
1. uname -a ~$ uname -a Linux lubuntu-Vostro-A840 3.19.0-73-generic #81-Ubuntu SMP Tue Oct 18 16:02: ...
- DEDE织梦,文章页里的幻灯调用,能调用全部栏目的吗
arclist 是必须要有 typeid 设置的,如果你没设置,默认是取的当前栏目的 typeid,而首页取到的是 top,所以你强制指定typeid=top就可以了.---------------- ...
- C++学习31 重载=(赋值运算符)
和普通变量一样,对象之间也可以相互赋值.赋值运算符“=”可以用来将一个对象拷贝给另一个已经存在的对象.对象之间的赋值是将成员变量依次拷贝,而不是将整个对象的内存按位拷贝. 对象之间的赋值: #incl ...
- C++学习11 类和new、delete操作符 类与const关键字
如果你是Java.C#.PHP程序员,那么会对 new 非常熟悉,在这些编程语言中,只能通过 new 来创建对象. 在C++中,你可以像定义变量一样来创建对象,如: Student stu; //对象 ...
- Java SE 第二十四讲----static与final使用陷阱关键字
1.对于final类型成员变量,一般来说有两种赋值方式: a)在声明final类型的成员变量时就附上初值 package com.cl.staticandfinal; public class Fin ...