直方图中最大面积

来源:互联网 发布:io是哪里的域名 编辑:程序博客网 时间:2024/05/01 01:00
 

Largest Rectangle in Histogram (直方图中最大面积) 【leetcode】

分类: leetcode面试算法题 131人阅读 评论(0) 收藏 举报
栈Largest Rectangle inleetcode

题目:

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)复杂度的做法,用栈维护一个递增的序列,栈中存对应高度的位置。

每遍历一个元素,判断是否是栈中最大的元素,如果不是,把栈顶的元素弹出,并计算以栈顶元素为最大值高度时的长方形面积。

面积的长度为栈顶元素之前的一个元素到当前遍历的元素的之间的长度,边界情况特殊考虑。


[cpp] view plaincopy
  1. class Solution {  
  2. public:  
  3.     int largestRectangleArea(vector<int> &height) {  
  4.         stack<int>s;  
  5.         int len=height.size(),maxx=0;  
  6.         for(int i=0;i<len;++i)  
  7.         {  
  8.             if(s.empty())s.push(i);  
  9.             else  
  10.             {  
  11.                 while(!s.empty()&&height[s.top()]>height[i])  
  12.                 {  
  13.                     int ph=s.top();  
  14.                     s.pop();  
  15.                     if(!s.empty())  
  16.                         maxx=max(maxx,(i-s.top()-1)*height[ph]);  
  17.                     else   
  18.                         maxx=max(maxx,i*height[ph]);                      
  19.                 }    
  20.                 s.push(i);  
  21.             }  
  22.         }  
  23.         while(!s.empty())  
  24.         {  
  25.             int ph=s.top();  
  26.             s.pop();  
  27.             if(!s.empty())  
  28.                  maxx=max(maxx,(len-s.top()-1)*height[ph]);  
  29.             else   
  30.                  maxx=max(maxx,len*height[ph]);    
  31.               
  32.         }  
  33.         return maxx;  
  34.     }  
  35. }; 
利用单调队列应该也是可以做的

Maximal Rectangle (求矩阵的最大的子矩阵) 【面试算法leetcode】

分类: leetcode面试算法题 108人阅读 评论(0) 收藏 举报
leetcode最大的子矩阵Maximal Rectangle

题目:

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介绍过这个步骤,就不细说。


[cpp] view plaincopy
  1. int height[1000][1000];  
  2. class Solution {  
  3. public:  
  4.     int maximalRectangle(vector<vector<char> > &matrix) {  
  5.          
  6.         int i,j,k,row,col,maxx=0;  
  7.         row=matrix.size();  
  8.         if(row==0)return 0;  
  9.         col=matrix[0].size();  
  10.         if(col==0)return 0;             
  11.         for(j=0;j<col;++j)  
  12.             for(i=0;i<row;++i)  
  13.                 if(matrix[i][j]=='0')height[i][j]=0;  不是连续的1 就要清零
  14.                 else if(i==0)height[0][j]=1;  
  15.                 else height[i][j]=height[i-1][j]+1;  
  16.           
  17.         stack<int>s;  
  18.         for(i=0;i<row;++i)  
  19.         {  
  20.             for(j=0;j<col;++j)  
  21.             {  
  22.                 if(s.empty())s.push(j);  
  23.                 else  
  24.                 {  
  25.                     while(!s.empty()&&height[i][s.top()]>height[i][j])  
  26.                     {  
  27.                         int ph=s.top();  
  28.                         s.pop();  
  29.                         if(!s.empty())  
  30.                             maxx=max(maxx,(j-s.top()-1)*height[i][ph]);  
  31.                         else   
  32.                             maxx=max(maxx,j*height[i][ph]);                      
  33.                     }    
  34.                     s.push(j);  
  35.                 }  
  36.             }  
  37.             while(!s.empty())  
  38.             {  
  39.                 int ph=s.top();  
  40.                 s.pop();  
  41.                 if(!s.empty())  
  42.                      maxx=max(maxx,(col-s.top()-1)*height[i][ph]);  
  43.                 else   
  44.                      maxx=max(maxx,col*height[i][ph]);    
  45.                   
  46.             }  
  47.         }  
  48.         return maxx;  
  49.     }  
  50. };  

原创粉丝点击