4.1.3LeetCode: Largest Rectangle in Histogram(直方图最大面积)
2015-06-01 18:06
656 查看
http://www.cnblogs.com/avril/archive/2013/08/24/3278873.html
具体的题目描述为:
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 =
The largest rectangle is shown in the shaded area, which has area =
For example,
Given height =
return
这个方法非常巧妙,最好通过一个图来理解:
假设输入直方图为:int[] height = {2,7,5,6,4}.
这个方法运行的时候,当遇到height[2] == 5的时候,发现其比之前一个高度小,则从当前值(5)开始,向左搜索比当前值小的值。当搜索到最左边(2)时,比5小,此时计算在height[0]和height[2]之间的最大面积,注意不包括height[0]和和height[2]。height[1]以红色标出的这个区域就被计算完成。同样的方法,计算出绿色和粉色的面积。
因此这个方法需要使用两个栈。第一个栈为高度栈heightStack,用于记录还没有被计算过的连续递增的序列的值。第二个栈为下标栈indexStack,用于记录高度栈中对应的每一个高度的下标,以计算宽度。
算法具体执行的步骤为:
若heightStack为空或者当前高度大于heightStack栈顶,则当前高度和当前下标分别入站(下面有一个解法可以只用一个栈即可,用栈来保存下标,而高度由下标很容易得到)。所以heightStack记录了一个连续递增的序列。
若当前高度小于heightStack栈顶,heightStack和indexStack出栈,直到当前高度大于等于heightStack栈顶。出栈时,同时计算区间所形成的最大面积。注意计算完之后,当前值入栈的时候,其对应的下标应该为最后一个从indexStack出栈的下标。比如height[2]入栈时,其对应下标入栈应该为1,而不是其本身的下标2。如果将其本身下标2入栈,则计算绿色区域的最大面积时,会忽略掉红色区域。
C++代码:
具体的题目描述为:
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 =
10unit.
For example,
Given height =
[2,1,5,6,2,3],
return
10.
这个方法非常巧妙,最好通过一个图来理解:
假设输入直方图为:int[] height = {2,7,5,6,4}.
这个方法运行的时候,当遇到height[2] == 5的时候,发现其比之前一个高度小,则从当前值(5)开始,向左搜索比当前值小的值。当搜索到最左边(2)时,比5小,此时计算在height[0]和height[2]之间的最大面积,注意不包括height[0]和和height[2]。height[1]以红色标出的这个区域就被计算完成。同样的方法,计算出绿色和粉色的面积。
因此这个方法需要使用两个栈。第一个栈为高度栈heightStack,用于记录还没有被计算过的连续递增的序列的值。第二个栈为下标栈indexStack,用于记录高度栈中对应的每一个高度的下标,以计算宽度。
算法具体执行的步骤为:
若heightStack为空或者当前高度大于heightStack栈顶,则当前高度和当前下标分别入站(下面有一个解法可以只用一个栈即可,用栈来保存下标,而高度由下标很容易得到)。所以heightStack记录了一个连续递增的序列。
若当前高度小于heightStack栈顶,heightStack和indexStack出栈,直到当前高度大于等于heightStack栈顶。出栈时,同时计算区间所形成的最大面积。注意计算完之后,当前值入栈的时候,其对应的下标应该为最后一个从indexStack出栈的下标。比如height[2]入栈时,其对应下标入栈应该为1,而不是其本身的下标2。如果将其本身下标2入栈,则计算绿色区域的最大面积时,会忽略掉红色区域。
C++代码:
class Solution { public: int largestRectangleArea(vector<int> &height) { if(height.size() == 0) return 0; int res = 0; vector<int> tmp = height; tmp.push_back(0); // Important stack<int> s; for(int i = 0; i < tmp.size(); i++) { if(s.empty() || (!s.empty() && tmp[i] >= tmp[s.top()])) s.push(i); else{ while(!s.empty() && tmp[s.top()] > tmp[i]) { int idx = s.top(); s.pop(); int width = s.empty() ? i : (i-s.top()-1); res = max(res, tmp[idx] * width); } s.push(i); // Important } } return res; } };
相关文章推荐
- C/C++中文件输入输出流的一个示例(使用list容器)
- C#泛型-使用委托的Sort排序方法
- 从符号表来理解指针和引用
- sharepoint adfs Adding Claims to an Existing Token Issuer in SharePoint 2010
- sharepoint adfs Adding Claims to an Existing Token Issuer in SharePoint 2010
- sharepoint adfs Adding Claims to an Existing Token Issuer in SharePoint 2010
- sharepoint adfs Adding Claims to an Existing Token Issuer in SharePoint 2010
- 网络分析工具
- 对图片的缓存
- 【iOS百度地图系列_0】百度地图的配置及实现基本地图
- 选择排序-最基本版本
- 进一步封装微信JS SDK,使开发人员对JS SDk透明
- 第13周 项目二-形状族中的纯虚函数
- mvc url路由参数的加密和解密
- JDK的动态代理-多接口
- Android OnLowMemory和OnTrimMemory
- Linux系统下文件的复制和移动命令
- 【云快讯】之四十四《IBM Watson在能源行业的新应用》
- Mac下Notes报错NSSQLiteErrorDomain error 8解决方法
- 网银接口的代码编写总结