用栈解决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 ...
随机推荐
- Android开发--仿微信语音对讲录音
原文地址:http://www.2cto.com/kf/201502/378704.html 自微信出现以来取得了很好的成绩,语音对讲的实现更加方便了人与人之间的交流.今天来实践一下微信的语音对讲的录 ...
- SQL日期格式,转自will哥
我之前一直認為 SQL Server 針對日期處理的函數不夠多(如果跟 MySQL 比較),尤其是處理日期欄位轉字串的時候,常常因為要輸出特定的格式而懊惱不已,常常一不小心就寫了一長串,很不易閱讀. ...
- 从SQL下载大量数据到Excel
之前不知设计原理,发生了大量数据(超过100w行)直接从数据库读取加载到网页中,直接导致内存溢出. Rediculous! 所以,现在改为分页查询到页面中. 由于其有全局逻辑,故折中每次加载1w条数据 ...
- bootstrap 时间控件带(时分秒)选择器
1.控件下载地址:http://www.bootcss.com/p/bootstrap-datetimepicker/index.htm,参数设置说明也在这个链接下面: 2.具体参数说明(复制原链接) ...
- MyEclipse配置Tomcat开发JavaWeb程序JSP以及Servlet
1.安装准备 1).下载安装MyEclipse2014,这已经是最新版本. 2).下载Tomcat 官网:http://tomcat.apache.org/ 我们选择8.0: http://tomca ...
- Robotium-无源码测试
[总结] 1.新建工程,选择Android Application Project,选择This Project: PS:新建测试工程时报NULL错误,新建一个Android工程,然后再按上诉步骤建立 ...
- 关于spring boot jar包与war包的问题
此文为转载:http://mrlee23.iteye.com/blog/2047968 在开发调试完成之后,可以将应用打成JAR包的形式,在Eclipse中可以直接使用Maven插件的package命 ...
- Window 命令
tracert XXX.XXX.XXX.XXX 路由追踪命令,可以显示到目的IP所经过的路由
- 深入Java虚拟机
第一章:Java体系结构介绍 1.Java为什么重要? Java是为网络而设计的,而Java这种适合网络环境的能力又是由其体系结构决定的,可以保证安全健壮和平台无关的程序通过网络传播. 2 ...
- jeasyui datagrid控件的一个小问题
页面上用了datagrid,但今天把easyui更新到1.4.2以后出了个错,Cannot read property 'width' of null,以前用1.3.6的时候没有这个问题. 由于表格中 ...