利用stack求柱状图的最大矩形面积
2018-01-08 21:14
405 查看
84. Largest
Rectangle in Histogram
问题描述:
Given n non-negativeintegers 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 heights =
[2,1,5,6,2,3],
return
10.
问题解析:
1. 本题的意思是:给定一个arr,在arr中找出面积最大的矩形。2. 最简单的暴力求解是,相当于找出一个左边界和右边界,在计算两个边界之间高度最小的矩形面积。这种方法一定能够找出最大面积,但是时间复杂度是O(n^2),不能AC。
3. 时间复杂度为O(n)的解法。设置一个辅助栈sta,从arr第0个数开始,当arr[i]大于等于sta.top()时,就压入栈,否则,说明sta.top()那个数的右边界已经找到,则弹出sta.top(),计算当前面积cur,并与最大面积max比较大小或替换。弹出top时,一直弹出到当前的top值小于或者等于arr[i]为止。并记录刚才弹出了几个top,就再次压入栈中几个arr[i],最后压入当前的arr[i]。
代码如下:
class Solution { public: // 利用堆栈来解决 int largestRectangleArea(vector<int>& heights) { if(heights.empty()) return 0; stack<int>sta; int cur = 0, max = 0; int len = 0; int size = heights.size(); for(int i=0; i<size; ++i) { if(sta.empty() || sta.top() <= heights[i]) sta.push(heights[i]); else { len = 0; while(!sta.empty() && sta.top() > heights[i]) { ++ len; cur = sta.top()*len; if(cur > max) max = cur; sta.pop(); } while(len) { --len; sta.push(heights[i]); } sta.push(heights[i]); } } while(!sta.empty()) { ++ len; cur = sta.top()*len; if(cur > max) max = cur; sta.pop(); } return max; } };
85. Maximal
Rectangle
问题描述:
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.For example, given the following matrix:
1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0
Return 6.
问题解析:
1. 此题是求矩阵中为1的最大的矩形元素个数。2. 此题可以化为上题中求柱状图最大面积。从矩阵第0行开始,每一行可以看成是一个heights数组,本行j位置为1,如果上一行此列为1,则heights[j] = height[j]+1,然后求最大矩形面积,利用上题代码。
代码如下:
class Solution { public: // 利用堆栈求最大的数字 void largestRectangleArea(vector<int>& heights, int &max) { if(heights.empty()) return; stack<int>sta; int cur = 0; int len = 0; int size = heights.size(); for(int i=0; i<size; ++i) { if(sta.empty() || sta.top() <= heights[i]) sta.push(heights[i]); else { len = 0; while(!sta.empty() && sta.top() > heights[i]) { ++ len; cur = sta.top()*len; if(cur > max) max = cur; sta.pop(); } while(len) { --len; sta.push(heights[i]); } sta.push(heights[i]); } } while(!sta.empty()) { ++ len; cur = sta.top()*len; if(cur > max) max = cur; sta.pop(); } return; } int maximalRectangle(vector<vector<char>>& matrix) { int m=matrix.size(), n=0; if(m>0) n=matrix[0].size(); int max = 0; vector<int> heights(n, 0); for(int i=0; i<m; ++i) { for(int j=0; j<n; ++j) { heights[j] = matrix[i][j]=='1'? heights[j]+1 : 0; } largestRectangleArea(heights, max); } return max; } };
相关文章推荐
- 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积
- 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形
- 柱状图内最大的矩形面积 Largest Rectangle in Histogram @LeetCode
- 【面试题】柱状图内最大的矩形面积
- 柱状图求最大矩形面积
- 通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型,能够利用接口和多态性计算几何图形的面积和周长并显示。
- HDU 2478 Slides(n个矩形中n-1个矩形相交最大面积)
- 【最大子矩形】面积
- 【POJ 1964】求10图矩形最大面积
- 数组最大矩形面积
- 庞果英雄会---寻找直方图中面积最大的矩形---编程挑战
- Largest Rectangle in a Histogram(动态规划找最大矩形面积)
- Terrible Sets(求矩形的最大面积(递增栈))
- 通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型, 能够利用接口和多态性计算几何图形的面积和周长并显示
- 柱状图中找最大矩形 & 矩阵中找最大的仅含相同值的矩形区域
- LeetCode 11. Container With Most Water --多个点的x轴垂直线与x轴围成的最大矩形面积
- 一条长为L的绳子,一面靠墙,另外三边组成矩形,问此矩形最大面积能是多少?
- 90页第三题,创建一个 Rectangle类,添加width和lenght两个成员变量 在 Rectangle类中添加两种方法分别计算矩形的周长和面积 ,编程利用Rectangle输出一个矩形的
- POJ2559 最大矩形面积
- 1102 面积最大的矩形(简单DP)