用栈解决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 ...
随机推荐
- kendoTreeView,需要注意的地方
今天使用了下kendoUI中的TreeView控件,一开始的时候总是不成功, 总是会报 Uncaught TypeError: Cannot read property 'toLowerCase' o ...
- 如何在Flex标签中写事件函数
在事件变量值中直接写函数语句,如果是多条语句,则用";"号隔开. 示例如下: <mx:Box id="label" backgroundColor=&qu ...
- 信鸽推送(XGPush)
先放入两个链接: iOS信鸽接入官方文档:http://developer.qq.com/wiki/xg/iOS接入/iOS%20SDK完整接入/iOS%20SDK完整接入.html 信鸽开放平台:h ...
- spring+junit单元测试
<1>读取文件: 配置文件在classes下:locations = {"classpath*:/spring/applicationContext.xml"} 配置文 ...
- [转载] python 计算字符串长度
本文转载自: http://www.sharejs.com/codes/python/4843 python 计算字符串长度,一个中文算两个字符,先转换成utf8,然后通过计算utf8的长度和len函 ...
- centos7安装apue.3e时出错处理
错误代码如下: /tmp/ccb9gvom.o: In function `thr_fn': barrier.c:(.text+0x6e): undefined reference to `heaps ...
- errorlevel
------siwuxie095 errorlevel 程序返回码 用法:echo %errorlevel% 查看当前程序返回码(即返回值),以知道程序或命令行是否执行成功. DOS在执行完后都有返回 ...
- 字节b换算kb/mb/gb/tb/pb
public static string HumanReadableFilesize(double size) { string[] units = new string[] { "B&qu ...
- Beta-1阶段成员贡献分(代组长更新)
组名:天天向上 组长:王森 小组成员:张金生.张政.林莉.胡丽娜 小组贡献分如下:王森5.2 张金生5.1 张政 5.0 林莉 4.9 胡丽娜 4.8 成员得分如下: 成员 基础分 表现分 ...
- JsonWriter使用
Example: 拼一个如下的json格式String { [ { "id": 912345678901, &quo ...