File tree Expand file tree Collapse file tree 2 files changed +46
-46
lines changed Expand file tree Collapse file tree 2 files changed +46
-46
lines changed Original file line number Diff line number Diff line change 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+ };
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments