直方图中最大面积
来源:互联网 发布:io是哪里的域名 编辑:程序博客网 时间:2024/05/01 01:00
Largest Rectangle in Histogram (直方图中最大面积) 【leetcode】
题目:
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
.
之前做过类似的题,有O(n)复杂度的做法,用栈维护一个递增的序列,栈中存对应高度的位置。
每遍历一个元素,判断是否是栈中最大的元素,如果不是,把栈顶的元素弹出,并计算以栈顶元素为最大值高度时的长方形面积。
面积的长度为栈顶元素之前的一个元素到当前遍历的元素的之间的长度,边界情况特殊考虑。
- class Solution {
- public:
- int largestRectangleArea(vector<int> &height) {
- stack<int>s;
- int len=height.size(),maxx=0;
- for(int i=0;i<len;++i)
- {
- if(s.empty())s.push(i);
- else
- {
- while(!s.empty()&&height[s.top()]>height[i])
- {
- int ph=s.top();
- s.pop();
- if(!s.empty())
- maxx=max(maxx,(i-s.top()-1)*height[ph]);
- else
- maxx=max(maxx,i*height[ph]);
- }
- s.push(i);
- }
- }
- while(!s.empty())
- {
- int ph=s.top();
- s.pop();
- if(!s.empty())
- maxx=max(maxx,(len-s.top()-1)*height[ph]);
- else
- maxx=max(maxx,len*height[ph]);
- }
- return maxx;
- }
- };
Maximal Rectangle (求矩阵的最大的子矩阵) 【面试算法leetcode】
题目:
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
题意有一个01组成的矩阵,找到其中面积最大的,全部由1构成的子矩阵。
去年做多校比赛的时候第一次见到这题,不优化到O(n×n)死活过不了当时。
优化就是先预处理成保存成,当前点向上都是1的最高的高度,就变成每一行都是一个直方图,
之后用O(n)的直方图求最大面积去算,之前一篇文章 http://blog.csdn.net/havenoidea/article/details/11854723介绍过这个步骤,就不细说。
- int height[1000][1000];
- class Solution {
- public:
- int maximalRectangle(vector<vector<char> > &matrix) {
- int i,j,k,row,col,maxx=0;
- row=matrix.size();
- if(row==0)return 0;
- col=matrix[0].size();
- if(col==0)return 0;
- for(j=0;j<col;++j)
- for(i=0;i<row;++i)
- if(matrix[i][j]=='0')height[i][j]=0; 不是连续的1 就要清零
- else if(i==0)height[0][j]=1;
- else height[i][j]=height[i-1][j]+1;
- stack<int>s;
- for(i=0;i<row;++i)
- {
- for(j=0;j<col;++j)
- {
- if(s.empty())s.push(j);
- else
- {
- while(!s.empty()&&height[i][s.top()]>height[i][j])
- {
- int ph=s.top();
- s.pop();
- if(!s.empty())
- maxx=max(maxx,(j-s.top()-1)*height[i][ph]);
- else
- maxx=max(maxx,j*height[i][ph]);
- }
- s.push(j);
- }
- }
- while(!s.empty())
- {
- int ph=s.top();
- s.pop();
- if(!s.empty())
- maxx=max(maxx,(col-s.top()-1)*height[i][ph]);
- else
- maxx=max(maxx,col*height[i][ph]);
- }
- }
- return maxx;
- }
- };
- 直方图中最大面积
- 直方图中最大矩形面积
- 直方图中最大矩形面积
- 寻找直方图中面积最大的矩形
- 寻找直方图中面积最大的矩形
- 寻找直方图中面积最大的矩形
- 寻找直方图中面积最大的矩形
- 寻找直方图中面积最大的矩形
- 寻找直方图中面积最大的矩形
- 寻找直方图中面积最大的矩形
- 寻找直方图中面积最大的矩形
- 寻找直方图中面积最大的矩形
- 寻找直方图中面积最大的矩形
- 【算法】直方图中最大面积问题
- 求直方图中最大矩形面积
- 直方图最大面积矩形
- 直方图最大面积 ---- DP
- 直方图最大矩形面积
- CTO理解
- 【C语言的学习】第五回合:运算符的优先级大集合
- Scala中国社区论坛开放
- C++内存管理(2)
- 给IOS初学者及新手的建议
- 直方图中最大面积
- MySQL判断表是否存在
- 黑动漫@黑客的反击
- 浅析NLS_COMP、NLS_SORT(三)
- WRK
- 内核同步方法二
- C++ AMP: Staging Arrays in C++ AMP
- 使用ArcGIS GP服务之五 JavaScript的调用
- 在规定次数内找出数组中最大值和最小值