-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathMaximal_Rectangle.cpp
35 lines (33 loc) · 1.09 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
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
int row = matrix.size();
if(row == 0) return 0;
int col = matrix[0].size();
vector<vector<int> > dp(row, vector<int>(col));
for(int i = 0; i < row; ++i) {
int k = 0;
for(int j = 0; j < col; ++j) {
dp[i][j] = matrix[i][j] == '1' ? k = k + 1 : k = 0;
}
}
int item, Max = 0;
for(int i = 0; i < row; ++i) {
for(int j = 0; j < col; ++j) {
if(dp[i][j]) {
item = dp[i][j];
Max = max(Max, item);
for(int k = i + 1, l = 2; k < row; k++, l++) {
if(dp[k][j]) {
item = min(dp[k][j], item);
Max = max(Max, item * l);
} else {
break;
}
}
}
}
}
return Max;
}
};