-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximal_rectangle.cpp
37 lines (37 loc) · 1.2 KB
/
maximal_rectangle.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
int maximalRectangle(vector<vector<char> > &matrix) {
if(matrix.size()==0) return 0;
// build table
int row = matrix.size(); int col = matrix[0].size();
vector<vector<int>> table(row, vector<int>(col, 0));
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
int count = 0;
for(int k=j; k<col; k++) {
if(matrix[i][k]=='1') {
count++;
}else{
break;
}
}
table[i][j] = count;
}
}
int max_area=0;
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
if(table[i][j]==0) continue;
int width=table[i][j];
int up = i;
while((up-1)>=0&&table[up-1][j]>=width) {
up--;
}
int down = i;
while((down+1)<row&&table[down+1][j]>=width) {
down++;
}
int height = down-up+1;
max_area = max(max_area, height*width);
}
}
return max_area;
}