Skip to content

Commit 5aa21e1

Browse files
committed
Update and rename maximalRectangle.cpp to maximal-rectangle.cpp
1 parent 77d4c5e commit 5aa21e1

File tree

2 files changed

+46
-46
lines changed

2 files changed

+46
-46
lines changed

C++/maximal-rectangle.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Time: O(m * n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int maximalRectangle(vector<vector<char> > &matrix) {
7+
if(matrix.empty())
8+
return 0;
9+
10+
const int m = matrix.size();
11+
const int n = matrix.front().size();
12+
13+
int ans = 0;
14+
15+
vector<int> H(n, 0); // height of all ones rectangle include matrix[i][j]
16+
vector<int> L(n, 0); // left closed bound of all ones rectangle include matrix[i][j]
17+
vector<int> R(n, n); // right open bound of all onces rectangle include matrix[i][j]
18+
19+
for(int i = 0; i < m; ++i) {
20+
int left = 0, right = n;
21+
for(int j = 0; j < n; ++j) {
22+
if(matrix[i][j] == '1') {
23+
++H[j]; // update height
24+
L[j] = max(L[j], left); // update left bound
25+
}
26+
else {
27+
left = j + 1;
28+
H[j] = L[j] = 0;
29+
R[j] = n;
30+
}
31+
}
32+
33+
for(int j = n - 1; j >= 0; --j) {
34+
if(matrix[i][j] == '1') {
35+
R[j] = min(R[j], right); // update right bound
36+
ans = max(ans, H[j] * (R[j] - L[j]));
37+
}
38+
else {
39+
right = j;
40+
}
41+
}
42+
}
43+
44+
return ans;
45+
}
46+
};

C++/maximalRectangle.cpp

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)