-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day-136.cpp
51 lines (51 loc) · 1.58 KB
/
Day-136.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution {
private:
int maxAns;
int area(vector<int>&his) {
int a{};
stack<int>s;
int i=0;
for(; i<his.size();) {
if (s.empty() || his[s.top()] <= his[i]) {
s.push(i++);
} else {
int t = s.top(); s.pop();
if (s.empty()) {
a = max(a , his[t] * i);
} else {
a = max(a , his[t] * (i - s.top() - 1));
}
}
}
while (!s.empty()) {
int t = s.top(); s.pop();
int area;
if (s.empty()) {
area = his[t] * (i);
} else{
area = his[t] * (i - s.top() -1);
}
a = max(a , area);
}
return a;
}
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if (matrix.size() ==0 || matrix[0].size()==0){
return 0;
}
maxAns = 0;
vector<int>histogram(matrix[0].size());
for(int row=0; row<matrix.size(); ++row) {
for(int col=0; col<matrix[row].size(); ++col) {
if (matrix[row][col]=='1') {
histogram[col]+=1;
} else {
histogram[col]=0;
}
}
maxAns =max(maxAns , area(histogram));
}
return maxAns;
}
};