forked from Anchor89/LeetCodeAnchor89
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaximalRectangle.cpp
37 lines (36 loc) · 1011 Bytes
/
MaximalRectangle.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
#include "LeetCode.h"
class Solution {
public:
int maxRow(vector<int>& row) {
if (row.empty()) return 0;
row.push_back(0);
typedef pair<int, int> Pair;
stack<Pair> s;
s.push(Pair(0, row[0]));
int size = row.size(), result = 0, lb;
for (int i=1; i<size; i++) {
lb = i;
while(!s.empty() && s.top().second > row[i]) {
lb = s.top().first;
result = max(result, s.top().second*(i-lb));
s.pop();
}
s.push(Pair(lb, row[i]));
}
return result;
}
int maximalRectangle(vector<vector<char> > &matrix) {
int row = matrix.size();
if (row == 0) return 0;
int col = matrix[0].size();
int result = 0;
vector<int> height(col, 0);
for_each(matrix.begin(), matrix.end(), [&](vector<char>& R) {
transform(R.begin(), R.end(), height.begin(), height.begin(), [](char c, int a) {
return c=='0'?0:a+1;
});
result = max(result, maxRow(height));
});
return result;
}
};