用栈解决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 ...
随机推荐
- svn的基线划分与管理
一.基线(服务端默认划分3条基线) trunk:表示开发时版本存放的目录,即在开发阶段的代码都提交到该目录上. branches:表示发布的版本存放的目录,即项目上线时发布的稳定版本存放在该目录中. ...
- 关于Jquery的delegate绑定事件无效
今天在做一个页面,用的是easyui页面有很多的tabs,里面都放了iframe 需要在load事件动态调整iframe高度 发现始终无法使用delegate来绑定load事件. 纠结了一下午发现了问 ...
- map集合键值对存储,键值不重复,值可以重复
import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.Li ...
- 【Android端 APP GPU过度绘制】GPU过度绘制及优化
一.Android端的卡顿 Android端APP在具体使用的过程中容易出现卡顿的情况,比如查看页面时出现一顿一顿的感受,切换tab之后响应很慢,或者具体滑动操作的时候也很慢. 二.卡顿的原因 卡顿的 ...
- [原创]NIOS小知识总结
本文记录了在使用NIOS中遇到的一些问题和相关的原因分析及解决办法,做个总结方便以后查阅.也希望可以帮到有同样问题的攻城狮.本文长期更新,遇到了就写下. 本人使用软件版本:QuartusII 13.0 ...
- 安装SQL SERVER开启SA用户登录的方法
家庭安装SQL SERVER开启SA用户登录的方法:(切记按照网址操作完后,最后一定要在"管理工具"的"服务"里把"SQL SERVER(MSSQL ...
- shell编程之流程控制
-d 判断该文件是否存在,并且是否为目录文件 -e 判断该文件是否存在 -f 判断该文件是否存在,并且是否为普通文件 形式 [ -e /home/cao/test.txt ] -r 文件 判断该文 ...
- POI读取Excel内容格式化
在用POI读取Excel内容时,经常会遇到数据格式化的问题. 比如:数字12365会变为12365.0;字符串数字123也会变为123.0,甚至会被变为科学计数法.另外日期格式化也是一个头疼的问题.其 ...
- iOS强制横屏
由于项目需求,需要整个项目页面都是竖屏,唯独一个折线图页面强制性横屏显示. 网上逛了许多帖子,也看了好多大神的提供的方法,都没能够实现本屌丝想要的效果.没办法自己研究自己搞,借鉴各路大神的思路,最后费 ...
- 开启 mysql 远程访问
如何开启MySQL的远程帐号-1)首先以 root 帐户登陆 MySQL 在 Windows 主机中点击开始菜单,运行,输入“cmd”,进入控制台,然后cd 进入MySQL 的 bin 目录下,然后输 ...