【Largest Rectangle in Histogram】cpp
题目:
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
.
代码:
class Solution {
public:
int largestRectangleArea(vector<int>& height) {
int ret = ;
const size_t len = height.size();
if (!len) return len;
int *dp_left = new int[len](), *dp_right = new int[len]();
dp_left[] = ;
dp_right[len-] = len-;
// dp_left : record the left most position of height arr that the curr element can reach
for ( size_t i = ; i < len; ++i ){
int left = i;
while ( left> && height[i]<=height[left-] ) left = dp_left[left-];
dp_left[i] = left;
}
// dp_right : vice versa
for ( int i = len-; i >; --i) {
int right = i;
while ( right<len- && height[i]<=height[right+] ) right = dp_right[right+];
dp_right[i] = right;
}
// get the largest rectangle
for ( size_t i = ; i < len; ++i ) ret = std::max( ret, (dp_right[i]-dp_left[i]+)*height[i] );
delete[] dp_left;
delete[] dp_right;
return ret;
}
};
tips:
采用dp的思路,主要参考 http://www.acmerblog.com/largest-rectangle-in-histogram-6117.html
遍历三次:
1. left to right : dp_left[i]数组存放height[i]向左最多能推到哪个位置
2. right to left : dp_right[i]数组存放height[i]向右最多能推到哪个位置
注意,不论是dp_left还是dp_right存放都是height数组的绝对位置(一开始dp_right一直存放相对位置,导致不能AC)
这里虽然for循环中又有while循环,但是复杂度并不是O(n²),原因并不是一个挨着一个跳的,而是用之前比较的结果(dp_left,dp_right)跳的。
3. 最后走一遍dp,记录最大值
==========================================
自己找虐,又去追了一下stack的解法。自己写了很久并没有通过,学习了下网上的代码。
class Solution {
public:
int largestRectangleArea(vector<int>& height) {
int ret = ;
height.push_back();
stack<int> sta;
for ( int i = ; i < height.size(); )
{
if ( sta.empty() || height[i]>height[sta.top()] )
{
sta.push(i++);
}
else
{
int tmp = sta.top();
sta.pop();
ret = std::max( ret, height[tmp]*(sta.empty() ? i:i-sta.top()- ));
}
}
return ret;
}
};
核心思想就是:维护一个递增的stack。
1. 一旦遇到不能维持递增stack的元素了,就逐个往外弹出,直到能压进去。
2. 往外弹一个元素,就意味着这个元素不能再留着了,因此就计算一下包含弹出的这个元素在内,最大的rectangle是多少。
这里可能有一个疑问:把这个元素弹出来,那万一这个元素跟后面的元素能配上,获得更大大面积了呢?
这个是不可能发生的,因为这个元素能弹出来,必然是在其后面遇上了比它小的元素(阻断了被弹出的元素与后面的联系),因此这种算法是合理的。完毕。
===============================================
第二次过这道题,只记得用递增的stack来做了。
(1)stack里面存的是元素的下标
(2)先弹出来栈顶的元素,再看新的栈顶的元素(刚弹出来的这个元素往前能推到哪里)
class Solution {
public:
int largestRectangleArea(vector<int>& height) {
if (height.size()==) return ;
height.push_back();
int ret = ;
stack<int> sta;
for ( int i=; i<height.size(); ++i )
{
if ( sta.empty() || height[sta.top()]<height[i] )
{
sta.push(i);
}
else
{
while ( !sta.empty() && height[sta.top()]>=height[i] )
{
int tmp = sta.top();
sta.pop();
if ( sta.empty() )
{
ret = max(ret, i*height[tmp]);
}
else
{
ret = max(ret, (i-sta.top()-)*height[tmp]);
}
}
sta.push(i);
}
}
return ret;
}
};
【Largest Rectangle in Histogram】cpp的更多相关文章
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 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】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...
- 47. Largest Rectangle in Histogram && Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- 关于LeetCode的Largest Rectangle in Histogram的低级解法
在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
随机推荐
- 必须会的SQL语句(八)数据库的完整性约束
实体完整性 1.建表时定义主键 Create table 表名 ( Sno int identity(1,1), Sname nvarchar(20), ...
- ubuntu12.04 下安装matlab2012
1.下载matlab2012a(例如:****.iso) 2.创建挂载目录 sudo mkdir /media/matlab 3.将当前目录切换到镜像文件的目录,然后将镜像文件挂载到刚刚创建的目录下 ...
- nginx url重写 rewrite实例
本文介绍下,在nginx中实现Url重写,学习rewrite的具体用法,有需要的朋友参考下吧. 原文地址:http://www.360doc.com/content/14/0202/20/142341 ...
- UI Button
iOS开发UI篇—Button基础 一.简单说明 一般情况下,点击某个控件后,会做出相应反应的都是按钮 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置 二.按钮的三种状 ...
- Android VideoView简单播放视频
给Android VideoView一个文件目录,就可以直接播放智能设备中的视频文件,现在以播放事先用手机拍好并重命名的视频文件test.mp4为例.(1) 需要在布局文件中写一个ViedoView: ...
- [.ashx檔?泛型处理例程?]基础入门#1....能否用中文教会我?别说火星文?
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/08/20/ashx_beginner_01.aspx [.ashx檔?泛型处理例程? ...
- 【Inno Setup】 Inno Setup 64位安装程序默认安装路径
在脚本中加入: ArchitecturesInstallIn64BitMode=x64 ArchitecturesAllowed=x64
- ThinkPHP3.2.2中开启REWRITE模式
1. 在项目配置文件(\Application\Common\Conf\config.php)中配置URL模式 <?php return array( //URL模式 , ); 2. 在Thin ...
- javascript 关于Date 时间类型 处理方法
上一篇博客中和大家分享了关于 字符串转时间类型 这一篇顺便整理下 javascript 中 Date 类型的一些方法 var time = new Date(); var year=time.getY ...
- oracle-审计导数
1.因审计需求,需要将MySQL.Oracle数据库中需要的表数据导入到SqlSERVER进行审计. 2.之前的方法: A. oracle组将表dump下来,进行压缩,传送到oracle导数服务器 ...