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.

class Solution {    
public:
     int maximalRectangle(vector<vector< char> > &matrix) 
    {
         int m=matrix.size();
         if(m== 0return  0;
        
         int n=matrix[ 0].size();        
         int ** seq= new  int*[m];
         for( int i= 0;i<m;i++)
            seq[i]= new  int[n];
         for( int i= 0;i<m;i++)
        {
             int index= 0;
             for( int j= 0;j<n;j++)
            {
                seq[i][j]=matrix[i][j]- ' 0 ';
                 if(j> 0 && matrix[i][j]== ' 1 ' && matrix[i][j- 1]== ' 1 ')
                    seq[i][j]=seq[i][j- 1]+ 1;
            }
        }
         int max= 0;
         for( int i= 0;i<n;i++)
        {
             for( int j= 0;j<m;j++)
            {
                 int min=seq[j][i];
                 for( int k=j;k<m;k++)
                {
                     if(min>seq[k][i]) min=seq[k][i];
                     if(min*(k-j+ 1)>max) max=min*(k-j+ 1);                    
                }
            }
        }
         return max;
    }
};  

你可能感兴趣的:(max)