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(n^2) 另一个是非常流弊聪明的O(n),两个都花了我很长时间去思考,最后看了这篇博文 才略有所得。
首先暴破在现在看来思路应该是很直观的,但是我当时还是一点头绪没有,故思考良久以后决定去找答案。先来看看暴力法。

min_h = 0
max_a = 0 for i = [0 len){
if (min_h > height[i]) then continue
min_h = height[i]
for j = [i len){
if (min_h > height[j]){
min_h = height[j]
}
max_a = max(max_a, min_h * (j-i+1))
}
}

这是表达思路的伪码,大概就是,以每个柱子为基点,向前依次遍历其余柱子,用当前最短的那根乘上此刻前进的距离来求得面积,同时更新最大面积。这样遍历下来是O(n^2)的,虽然没有遗漏,但是也有很多不必要的枚举。
代码中 if (min_h > height[i]) then continue 这句是我尝试的一个优化,因为如果此趟遍历的高度都不超过前一趟的最小高度的话,这一趟得到的最大面积绝不会比前一趟大。但是这个优化仍是收效甚微,大集合没有通过。
至此也没有想到其他的优化手段,但是确实有人用优化过的暴力方法通过了大集合测试...我战斗力还是没有突破5啊。

暴力完整代码(Time Limit Exeeded):

 int largestRectangleArea(vector<int>height){
int maxArea=, minHeight=;
for (int i=; i<height.size(); i++){
if(height[i] <= minHeight)continue;
minHeight = height[i];
for (int j=i; j<height.size(); j++){
if (height[j] < minHeight){
minHeight = height[j];
}
int curArea = (j-i+) * minHeight;
if (curArea > maxArea){
maxArea = curArea;
}
}
}
return maxArea;
}

下面看看那个非常聪明流弊的O(n)解,还是建议去看一下开头提到的那篇博客,虽然几个图画的有问题,但是不影响理解的。
首先,他用一个栈来维护一个单调递增序列。 即在遍历过程中,遇到比栈顶大的才push。注意栈里保存的是索引。
如果遇到比栈顶小的,pop出栈顶并以他对应的实际数据为height,乘以一个width,求得面积并更新最大面积。那个width就是当前遍历位置i当前栈顶(注意已经pop过一次)的间距(不包括当前栈顶位置)
这样到遍历完成的时候我们就可以得到最大面积,因为每次弹栈,我们都确保了弹出的那个家伙获得了他组成的最大面积。噢,为了确保最后把栈弹空,我们需要在原数据后面加一个dummy的0。

这是AC的:

 int largestRectangleArea2(vector<int>height){
stack<int> s;
int maxArea = ;
int i=;
height.push_back();//dummy
int len = height.size();
while (i < len){
if (s.empty() || height[s.top()] < height[i]){
s.push(i++);
}else {
int h = s.top();
s.pop();
maxArea = max(maxArea, s.empty()? i*height[h] : height[h] * (i - s.top() - ));
}
}
return maxArea;
}

[LeetCode] Largest Rectangle in Histogram的更多相关文章

  1. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  2. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  3. LeetCode: Largest Rectangle in Histogram 解题报告

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  4. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  5. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  6. [leetcode]Largest Rectangle in Histogram @ Python

    原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...

  7. leetcode -- Largest Rectangle in Histogram TODO O(N)

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  8. [LeetCode] Largest Rectangle in Histogram 解题思路

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  9. LeetCode——Largest Rectangle in Histogram

    Question Given n non-negative integers representing the histogram's bar height where the width of ea ...

随机推荐

  1. Python自动化之sqlalchemy

    如果该数 据库支持 自增列 ,则 SQLAlchemy 默认 自动 设定 表中第一个 类型 为整形 的主键 为自增列 ORM介绍 orm英文全称object relational mapping,就是 ...

  2. centos 无线网卡安装,网卡rtl8188ee

    驱动: http://www.realtek.com.tw/downloads/downloadsView.aspx?Langid=1&PNid=48&PFid=48&Leve ...

  3. 基础知识《八》---Java反射机制

    1.反射可以做什么 2.反射相关的API 3.获取Class运行时类的实例的三种方法: 1)通过运行时类本身的.class属性***** Class clazz= Person.class; 2)通过 ...

  4. js弹出提示信息,然后跳转到另一页面

    <script language="javascript">  alert("您的用户名与密码已成功修改!");  document.locatio ...

  5. Unity3d 检查哪些prefab引用了某个UIAtlas

    适用情景:策划在用NGUI制作UI prefab时经常会使用一些临时的Atlas,然后再想改就不知道都哪些使用了.现在想修改下使用临时资源的GameObject 使用方式,先选中某个prefab或者某 ...

  6. 推荐一篇 OAuth 2.0 必读文章

    http://www.cnblogs.com/artech/p/oauth-03.html 共计有3篇相关内容,请仔细阅读! 再说一下我用box api 开发时的问题,在 box 程序登记页面: 对于 ...

  7. linux学习之-vsftp

    一.简介 vsftp是一个基于GPL发布的类unix系统上使用的ftp服务器软件,它的全称是very secure FTP ,软件的编写初衷是为了代码的安全,另外高速与高稳定性也是vsftp的两个重要 ...

  8. FFmpeg 官方 20160227 之后 追加 libmfx 无法在 xp 上运行的解决方法

    修改三个地方 _wfopen_s _wfopen strncpy_s strncpy swscanf_s swscanf 下载 fixffmpeg.7z, fixff.cmd FixFFmpeg.ex ...

  9. Divide and conquer:Telephone Lines(POJ 3662)

    电话线 题目大意:一堆电话线要你接,现在有N个接口,总线已经在1端,要你想办法接到N端去,电话公司发好心免费送你几段不用拉网线,剩下的费用等于剩余最长电话线的长度,要你求出最小的费用. 这一看又是一个 ...

  10. 页面上有两个元素id相同,js中如何取值

    页面上有两个table,id都是”cont2",现要在js中取到这两个table,改变样式. js实现: var tab2=document.all.cont2(1);var  tab=do ...