forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
26 lines (26 loc) · 825 Bytes
/
Copy paths1.cpp
File metadata and controls
26 lines (26 loc) · 825 Bytes
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
// OJ: https://leetcode.com/problems/maximal-rectangle/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(N)
class Solution {
public:
int maximalRectangle(vector<vector<char>>& A) {
if (A.empty() || A[0].empty()) return 0;
int M = A.size(), N = A[0].size(), ans = 0;
vector<int> H(N + 1);
for (int i = 0; i < M; ++i) {
stack<int> s;
for (int j = 0; j <= N; ++j) {
H[j] = j < N && A[i][j] == '1' ? H[j] + 1 : 0;
while (s.size() && H[s.top()] >= H[j]) {
int h = H[s.top()];
s.pop();
int w = s.size() ? (j - s.top() - 1) : j;
ans = max(ans, w * h);
}
s.push(j);
}
}
return ans;
}
};