栈——largest-rectangle-in-histogram求柱形图中的最大矩形面积
来源:互联网 发布:淘宝退货业务流程图 编辑:程序博客网 时间:2024/05/29 04:01
题目描述
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],
return10.
方法一:
动态规划:求以每一个height[i]为高的向左向右延伸的最大矩形面积,再求出其中的最大值。
public class Solution { public int largestRectangleArea(int[] height) { if(height == null||height.length == 0) return 0; //存放每一个height[i]的矩形面积 int[] area=new int[height.length]; for(int i=0;i<height.length;i++) { int curarea=height[i]; area[i]=height[i]; //向右边加 for(int j=i+1;j<height.length;j++) { if(height[j]<curarea) break; else area[i]+=curarea; } //向左边加 for(int k=i-1;k>=0;k--) { if(height[k]<curarea) break; else area[i]+=curarea; } } //找出area[i]中最大的值 int max=0; for(int i=0;i<area.length;i++) { if(area[i]>max) max=area[i]; } return max; }}
方法二:栈
http://www.cnblogs.com/ganganloveu/p/4148303.html
网上看到一种借助栈的做法,代码很漂亮,但是解释都非常模糊,我看懂之后,决定仔细描述思路如下:
1、如果已知height数组是升序的,应该怎么做?
比如1,2,5,7,8
那么就是(1*5) vs. (2*4) vs. (5*3) vs. (7*2) vs. (8*1)
也就是max(height[i]*(size-i))
2、使用栈的目的就是构造这样的升序序列,按照以上方法求解。
但是height本身不一定是升序的,应该怎样构建栈?
比如2,1,5,6,2,3
(1)2进栈。s={2}, result = 0
(2)1比2小,不满足升序条件,因此将2弹出,并记录当前结果为2*1=2。
将2替换为1重新进栈。s={1,1}, result = 2
(3)5比1大,满足升序条件,进栈。s={1,1,5},result = 2
(4)6比5大,满足升序条件,进栈。s={1,1,5,6},result = 2
(5)2比6小,不满足升序条件,因此将6弹出,并记录当前结果为6*1=6。s={1,1,5},result = 6
2比5小,不满足升序条件,因此将5弹出,并记录当前结果为5*2=10(因为已经弹出的5,6是升序的)。s={1,1},result = 10
2比1大,将弹出的5,6替换为2重新进栈。s={1,1,2,2,2},result = 10
(6)3比2大,满足升序条件,进栈。s={1,1,2,2,2,3},result = 10
栈构建完成,满足升序条件,因此按照升序处理办法得到上述的max(height[i]*(size-i))=max{3*1, 2*2, 2*3, 2*4, 1*5, 1*6}=8<10
综上所述,result=10
import java.util.*;public class Solution { public int largestRectangleArea(int[] height) { if(height == null||height.length == 0) return 0; Stack<Integer> stack=new Stack(); int ret=0; for(int i=0;i<height.length;i++) { if(stack.empty()||height[i]>=stack.peek()) { stack.push(height[i]); } else { int count=0; while(!stack.empty()&&stack.peek()>height[i]) { count++; ret=Math.max(ret,stack.peek()*count); stack.pop(); } while(count>0) { stack.push(height[i]); count--; } stack.push(height[i]); } } int count=1; while(!stack.empty()) { ret=Math.max(ret,stack.peek()*count); stack.pop(); count++; } return ret; }}
- 栈——largest-rectangle-in-histogram求柱形图中的最大矩形面积
- Largest Rectangle in Histogram 一排矩形中的最大的面积
- Leetcode刷题记—— 84. Largest Rectangle in Histogram(柱形图中最大矩形面积)
- [LeetCode]—Largest Rectangle in Histogram 求直方图最大填充矩形面积
- Largest Rectangle in Histogram 直方图中最大的矩形面积
- 最大矩形面积 84 largest rectangle in histogram
- Poj 2559 Largest Rectangle in a Histogram(柱形统计图中的最大矩形面积)
- HDU 1506 Largest Rectangle in a Histogram(最大矩形面积、单调栈)
- poj 2559 Largest Rectangle in a Histogram 求面积最大的矩形(单调栈)
- POJ 2559 Largest Rectangle in a Histogram(单调栈维护最大矩形面积)
- 寻找直方图中的最大矩形 Largest Rectangle in Histogram
- POJ 2559 &&HDU 1506 Largest Rectangle in a Histogram && 51nod 1102 面积最大的矩形 单调栈的应用
- 柱状图内最大的矩形面积 Largest Rectangle in Histogram @LeetCode
- LeetCode | Largest Rectangle in Histogram(直方图围城的最大矩形面积)
- HDU 1056 Largest Rectangle in a Histogram(dp)(求最大的矩形面积)
- [C++]LeetCode: 133 Largest Rectangle in Histogram(最大矩形面积)
- HDU 1506 Largest Rectangle in a Histogram (最大子矩形面积)
- POJ 2559 Largest Rectangle in a Histogram (DP最大矩形面积)
- [arcengine][栅格]裁剪栅格
- div layout
- HADOOP1.X与HADOOP2.X的区别
- PowerPoint2003制作抛物线动画的方法
- 详解MySQL 中explain解释命令
- 栈——largest-rectangle-in-histogram求柱形图中的最大矩形面积
- linux基本命令(55)——traceroute命令
- Android的px、dp和sp
- 如何快速实现浅层语义解析
- Populating Next Right Pointers in Each NodeII
- JSONObject.fromObject--JSON与对象的转换
- 【Android基础知识】Service
- QQ空间分享
- opencv 在图片中找对应矩形(形状分析)