用栈解决Largest Rectangle问题
一问题描述
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.
简单的翻译,就是求给的一连串长矩形的最大面积
二解决算法
本题解法很多,由于最近在学数据结构,故尝试用栈解决此题。
基本算法步骤:
1.将max_area置为0,声明一个栈s,s为存储对应方块的下标,且s为递增栈,具体做法将在下面继续说明
2.若栈为空或者当前矩形高度大于s的头元素对应的矩形高度,则其下标入栈,否则出栈,并计算出栈元素对应矩形高度的面积,更新max_area,直至当前头元素对应矩形高度小于将要入栈的矩形。
3.若最后栈非空,则将栈中元素一一出栈,并计算相应的面积,更新max_area。
4.最后的max_area为所求。
三算法原理说明
1.关于步骤2中出栈元素求其面积:
由于栈s为递增栈,故当当前矩形高度小于s中头元素(设为i)对应的矩形高度时,s出栈,出栈后s(若不空)的头元素设为top1,则出栈矩形对应的宽度为(i - top1 - 1),画个图更便于理解,注意s空时,应为i。
2.关于步骤3中出栈元素求其面积:
对应于s出栈元素对应矩形的宽度应为size - top1 - 1(栈不空,size为整个输入矩形的宽度,top1同上定义),注意若栈空,则宽度为size。
四代码示例
int largestRectangleArea(vector<int>& height) {
int maxArea =0, top = 0, top1 = 0;
int size = height.size();
stack<int> sInt;
if(!height.empty())
{
for(int i = 0; i < size; i++)
{
if(sInt.empty() || height[sInt.top()] < height[i])
sInt.push(i);
else
{
if(!sInt.empty())
top = sInt.top();
sInt.pop();
if(!sInt.empty())
top1 = sInt.top();
int width = sInt.empty()? i: i - top1 - 1;
maxArea = max(maxArea, height[top] * width);
i--;
}
}
}
//栈非空
while(!sInt.empty())
{
top = sInt.top();
top1 = 0;
sInt.pop();
if(!sInt.empty())
top1 = sInt.top();
int width = (sInt.empty())? size: (size - top1 - 1);
maxArea = max(maxArea, height[top] * width);
}
return maxArea;
用栈解决Largest Rectangle问题的更多相关文章
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- poj 2559 Largest Rectangle in a Histogram (单调栈)
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
- POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15831 ...
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
L ...
- Largest Rectangle in a Histogram HDU - 1506 (单调栈)
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...
- Largest Rectangle in a Histogram POJ - 2559 (单调栈)
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
- poj2559 Largest Rectangle in a Histogram(单调栈)
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
随机推荐
- 关于C#程序无故退出
今天我发现一种情况,分享下 我一个对象是用多线程写的代码,主程序调用完后有时候也会退出,catch不到.我在原对象的接口里面加上lock之后就ok了!我的理解是该对象申请的资源没释放完毕,加lock后 ...
- 1.springMVC+spring+Mybatis的整合思路
SSM整合的过程:就是把一些东西交给spring管理,也就是添加配置文件的一个过程.那么有哪些东西我们要交给spring管理呢?大概有以下几个: 1.数据源(可配置数据库连接池) 2.SqlSessi ...
- Net力软快速信息化系统开发框架 + 开发手册+数据库说明
源码目录结构说明LeaRun.Cache –缓存层LeaRun.Resource –本地语言LeaRun.Utilities –公共类库LeaRun.DataAccess –数据库访问核心组件LeaR ...
- 对于.h文件和.c文件
C语言中.h文件和.c文件详细解析_云止水_新浪博客http://blog.sina.com.cn/s/blog_73006d600102wcx5.html
- Struts2 的 值栈和ActionContext
1.ValueStack 和 ActionContext 的关系与区别: -- 相同点:它们都是在一次HTTP请求的范围内使用的,它们的生命周期都是一次请求 -- 不同点:ValueStack 分为对 ...
- MyBatis怎么防止SQL注入
SQL注入是一种代码注入技术,用于攻击数据驱动的应用,恶意的SQL语句被插入到执行的实体字段中(例如,为了转储数据库内容给攻击者).[摘自] SQL injection - Wikipedia SQL ...
- allegro - 层叠相关参数
层叠结构设置 弹出Layout Cross Section对话框 Subclass Name一列是该层的名称,可以按照自己的需要来填写.Type 列选择该层的类型,有三种: ·CONDUCTOR: ...
- android之handle
Android中异步消息处理主要由四个部分组成,Message.handler.messageQueue和looper. 1.message message是线程之间传递的消息,他可以在内部携带少量的 ...
- c++ cout介绍与实现自己的cout
C++编程语言互换流中的标准输出流,需要iostream支持.读为 "c out([si:‘aʊt]". 名字 cout 类型 std::ostream 读为 "c ou ...
- Activity之间传递参数(一)
-------siwuxie095 传递简单数据 (1)首先创建一个项目:SendArgs (2)选择API:21 Android 5.0 (3)选择 Empty Activity (4)默认 (5) ...