-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmaximal_rectangle.cpp
More file actions
83 lines (70 loc) · 1.91 KB
/
maximal_rectangle.cpp
File metadata and controls
83 lines (70 loc) · 1.91 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
76
77
78
79
80
81
82
83
class Solution {
public:
int maximal_rectangle(vector<int> heights) {
// 下面的代码参考largest_rectangle_in_histogram.cpp
// 变量名略作规范
int size = heights.size();
if (0 == size) {
return 0;
}
int result = INT_MIN;
int index;
stack<int> bars;
for (int i = 0; i < size; ++i) {
if (!bars.empty()) {
// 如果堆栈顶部的元素比当前的高度高,弹出并计算可能的面积
while ((!bars.empty()) && (heights[bars.top()] > heights[i])) {
index = bars.top();
bars.pop();
if (bars.empty()) {
result = max(result, i * heights[index]);
}
else {
result = max(result, (i - bars.top() - 1) * heights[index]);
}
}
}
// 前面的元素必须小于等于当前元素
bars.push(i);
}
while (!bars.empty()) {
index = bars.top();
bars.pop();
if (bars.empty()) {
result = max(result, size * heights[index]);
}
else {
result = max(result, (size - bars.top() - 1) * heights[index]);
}
}
return result;
}
int maximalRectangle(vector<vector<char> > &matrix) {
if (0 == matrix.size()) {
return 0;
}
int result = INT_MIN;
int height = matrix.size();
int width = matrix[0].size();
int row;
int col;
vector<vector<int> > counts(height, vector<int>(width, 0));
for (row = 0; row < height; ++row) {
for (col = 0; col < width; ++col) {
if (matrix[row][col] != '0') {
if (0 == row) {
counts[row][col] = 1;
}
else {
// 每一行往上数,包括自己有几个1。
counts[row][col] = counts[row - 1][col] + 1;
}
}
}
}
for (row = 0; row < height; ++row) {
result = max(result, maximal_rectangle(counts[row]));
}
return result;
}
};