-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
76 lines (73 loc) · 2.27 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Solution
{
public:
bool** rectangle;
int maximalRectangle(vector<vector<char> > &matrix)
{
if(matrix.size()==0||matrix[0].size()==0)
return 0;
int n = matrix.size();
int m = matrix[0].size();
rectangle = new bool*[n];
int max=0;
for(int i=0;i<n;i++)
{
rectangle[i] = new bool[m];
}
for(int k=0;k<n;k++)
{
for(int l=0;l<m;l++)
{
rectangle[k][l]=(matrix[k][l]=='1');
for(int i=k;i<n;i++)
{
bool end = false;
for(int j=l;j<m;j++)
{
if(i==k&&j==l)
{
if(rectangle[i][j])
{
int area = 1;
if(area > max)
max = area;
}
else
{
end = true;
break;
}
}
else if(i==k)
{
rectangle[i][j] = rectangle[i][j-1] && (matrix[i][j]=='1');
}
else if(j==l)
{
rectangle[i][j] = rectangle[i-1][j] && (matrix[i][j]=='1');
}
else
{
rectangle[i][j] = rectangle[i][j-1] && rectangle[i-1][j] && (matrix[i][j]=='1');
}
if(rectangle[i][j])
{
int area = (i-k+1)*(j-l+1);
if(area > max)
max = area;
}
else
{
break;
}
}
if(end)
{
break;
}
}
}
}
return max;
}
};