Maximal Rectangle LeetCode

 

https://leetcode.com/problems/maximal-rectangle/description/

 

同学推荐此题。

不能放main函数。

数据有可能为空,涨姿势了。。。

 

P.S: data in https://github.com/stomakun/LeetCodeTestData

 

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 #define ll long long
  4 #define minv 1e-6
  5 #define inf 1e9
  6 #define pi 3.1415926536
  7 #define nl 2.7182818284
  8 const ll mod=1e9+7;//998244353
  9 const int maxn=1e7+10;
 10 
 11 class Solution {
 12 public:
 13     char **str;
 14     int **a,*x,*y,g=0;
 15     vector<vector<char> >::iterator u;
 16     vector<char>::iterator v;
 17     vector<char> w;
 18     int maximalRectangle(vector<vector<char>>& matrix) {
 19         int n,m,i,j,sum;
 20         n=matrix.size();
 21         if (n==0)
 22             m=0;
 23         else
 24             m=matrix[0].size();
 25 
 26         str=new char*[n+1];
 27         for (i=0;i<=n;i++)
 28             str[i]=new char[m+1];
 29 
 30         a=new int*[n+1];
 31         for (i=0;i<=n;i++)
 32             a[i]=new int[m+1];
 33         x=new int[m+1];
 34         y=new int[m+1];
 35         for (u=matrix.begin(),i=1;u!=matrix.end();u++,i++)
 36         {
 37             w=*u;
 38             for (v=w.begin(),j=1;v!=w.end();v++,j++)
 39                 str[i][j]=*v;
 40         }
 41 
 42         for (i=0;i<=n;i++)
 43             a[i][0]=0;
 44         for (j=0;j<=m;j++)
 45             a[0][j]=0;
 46         for (j=1;j<=m;j++)
 47             for (i=1;i<=n;i++)
 48                 a[i][j]=(str[i][j]=='1')?a[i-1][j]+1:0;
 49         sum=0;
 50         for (i=1;i<=n;i++)
 51         {
 52             g=0;
 53             for (j=1;j<=m;j++)
 54                 if (g==0 || a[i][j]>x[g])
 55                 {
 56                     g++;
 57                     x[g]=a[i][j];
 58                     y[g]=j;
 59                 }
 60                 else
 61                 {
 62                     while (g!=0 && a[i][j]<x[g])
 63                         sum=max(sum,(j-y[g])*x[g]),g--;
 64                     if (g==0 || a[i][j]!=x[g])
 65                         g++;
 66                     x[g]=a[i][j];
 67                 }
 68             while (g!=0)
 69                 sum=max(sum,(m+1-y[g])*x[g]),g--;
 70         }
 71         return sum;
 72     }
 73 }sol;
 74 
 75 /*
 76 int main()
 77 {
 78     vector<vector<char>> matrix;
 79     vector<char> ch;
 80     matrix.clear();
 81 
 82 //    ch.clear();
 83 //    ch.push_back('1');
 84 //    ch.push_back('0');
 85 //    ch.push_back('1');
 86 //    ch.push_back('0');
 87 //    ch.push_back('0');
 88 //    matrix.push_back(ch);
 89 //
 90 //    ch.clear();
 91 //    ch.push_back('1');
 92 //    ch.push_back('0');
 93 //    ch.push_back('1');
 94 //    ch.push_back('1');
 95 //    ch.push_back('1');
 96 //    matrix.push_back(ch);
 97 //
 98 //    ch.clear();
 99 //    ch.push_back('1');
100 //    ch.push_back('1');
101 //    ch.push_back('1');
102 //    ch.push_back('1');
103 //    ch.push_back('1');
104 //    matrix.push_back(ch);
105 
106 //    ch.clear();
107 //    ch.push_back('1');
108 //    ch.push_back('0');
109 //    ch.push_back('0');
110 //    ch.push_back('1');
111 //    ch.push_back('0');
112 //    matrix.push_back(ch);
113 
114     cout<<sol.maximalRectangle(matrix);
115     return 0;
116 }
117 */

 

posted @ 2018-10-23 12:49  congmingyige  阅读(132)  评论(0编辑  收藏  举报