-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaximal_rectangle.cpp
More file actions
75 lines (72 loc) · 2.19 KB
/
maximal_rectangle.cpp
File metadata and controls
75 lines (72 loc) · 2.19 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
/**
* Desribe: Given a 2D boolean matrix filled with False and True, find the largest
* rectangle containing all True and return its area.
*/
class Solution {
public:
int maximalRectangle(vector<vector<bool> > &matrix) {
if (matrix.empty()) {
return 0;
}
int rows = matrix.size();
int cols = matrix[0].size();
vector<int> record(cols, 0);
// stack to record the column with consecutive True more than current
// column
stack<pair<int, int>> his;
int max = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (matrix[i][j]) {
record[j] += 1;
} else {
record[j] = 0;
}
int cum = 0;
// pop all columns with height greater or equal to current.
while (!his.empty() && his.top().first >= record[j]) {
int tmp = his.top().first * (his.top().second + cum);
max = max > tmp? max : tmp;
cum += his.top().second;
his.pop();
}
if (record[j] > 0) {
his.push(make_pair(record[j], cum + 1));
}
}
int cum = 0;
// empty the stack
while (!his.empty()) {
int tmp = his.top().first * (his.top().second + cum);
max = max > tmp? max : tmp;
cum += his.top().second;
his.pop();
}
}
return max;
}
};
int main() {
Solution so;
vector<vector<bool>> test;
int rows, cols;
int tmp;
while (cin >> rows) {
cin >> cols;
test = vector<vector<bool>>(rows, vector<bool>(cols, false));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < rows; ++j) {
cin >> tmp;
test[i][j] = tmp == 0? false : true;
}
}
auto re = so.maximalRectangle(test);
cout << "result: " << re << endl;
}
return 0;
}