用栈解决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 ...
随机推荐
- VmWare为Fedora虚拟机扩展磁盘
1.根据步骤为虚拟机扩展磁盘空间. 查看步骤 2.进入Fedora系统 以下步骤为修改扇区,将扩展磁盘划分为分区 运行fdisk -l,命令执行结果的第一行显示了磁盘名称,同时可以看到当前磁盘分区情况 ...
- angular中ng-model,返回数据,拆分数据,展示,名称相同,重新赋值会有冲突
本问题出在angular,1.X版本,我用的是1.5的版本: 问题原因: <input type="number" ng-mode="a" /> & ...
- unity3d 知识点随记
1.transform.translate是增加transform面板相应的数值x,y,z是以本地坐标系为方向:transform.transformdirection是以世界坐标系为方向,可以去测试 ...
- 2.struts2访问web资源(在struts2中获取session,request等等)
什么是web资源:web资源就是指request,response,session,servlet的api 为什么需要访问web资源:因为图片上传,需要获取图片的目录,就需要通过action来访问we ...
- JS正则表达式验证账号、手机号、电话和邮箱
JS正则表达式验证账号.手机号.电话和邮箱 效果体验:http://keleyi.com/keleyi/phtml/jstexiao/15.htm 验证帐号是否合法 验证规则:字母.数字.下划线组成, ...
- Win7如何显示/隐藏Administrator账号
为了保证安全,windows7的administrator账户默认在登录时是不显示的. 在登录页面显示administrator账户的方法: 1. 选择"开始"菜单->&qu ...
- iOS之UILabel自适应高度、宽度
下列两条自适应高度和宽度的自定义方法:
- 第13章 .NET应用程序配置
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...
- iOS开发中对RunLoop的个人心得
从接触iOS到现在也有将近两年了,对iOS中的RunLoop也有了一定的认识,下面讲讲个人对RunLoop的理解. 初识RunLoop RunLoops是与线程相关联的基础部分,一个Run Loo ...
- 安装SQL SERVER开启SA用户登录的方法
家庭安装SQL SERVER开启SA用户登录的方法:(切记按照网址操作完后,最后一定要在"管理工具"的"服务"里把"SQL SERVER(MSSQL ...