-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJan_11(85)
More file actions
54 lines (46 loc) · 1.55 KB
/
Copy pathJan_11(85)
File metadata and controls
54 lines (46 loc) · 1.55 KB
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
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if (matrix.empty() || matrix[0].empty()) return 0;
int M = matrix.size();
int N = matrix[0].size();
// convert char to int (in-place)
vector<vector<int>> mat(M, vector<int>(N));
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
mat[i][j] = matrix[i][j] - '0';
}
}
// row-wise prefix widths
for (int i = 0; i < M; i++) {
for (int j = 1; j < N; j++) {
if (mat[i][j] == 1) {
mat[i][j] += mat[i][j - 1];
}
}
}
int Ans = 0;
// fix each column
for (int j = 0; j < N; j++) {
for (int i = 0; i < M; i++) {
int width = mat[i][j];
if (width == 0) continue;
// expand downward
int currWidth = width;
for (int k = i; k < M && mat[k][j] > 0; k++) {
currWidth = min(currWidth, mat[k][j]);
int height = k - i + 1;
Ans = max(Ans, currWidth * height);
}
// expand upward
currWidth = width;
for (int k = i; k >= 0 && mat[k][j] > 0; k--) {
currWidth = min(currWidth, mat[k][j]);
int height = i - k + 1;
Ans = max(Ans, currWidth * height);
}
}
}
return Ans;
}
};