-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path85.cpp
83 lines (73 loc) · 2.16 KB
/
85.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
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
__________________________________________________________________________________________________
8ms
static const auto __ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
int rows = matrix.size();
if (rows == 0) return 0;
int cols = matrix[0].size();
if (cols == 0) return 0;
vector<int> left(cols, 0);
vector<int> right(cols, cols);
vector<int> height(cols, 0);
int area = 0;
for (int i = 0; i < rows; ++i) {
int current_left = 0;
int current_right = cols;
for (int j = 0; j < cols; ++j) {
if (matrix[i][j] == '1') {
++height[j];
left[j] = max(left[j], current_left);
} else {
height[j] = 0;
left[j] = 0;
current_left = j + 1;
}
}
for (int j = cols - 1; j >=0; --j) {
if (matrix[i][j] == '1') {
right[j] = min(right[j], current_right);
} else {
right[j] = cols;
current_right = j;
}
}
for (int j = 0; j < cols; ++j)
area = max(area, (right[j] - left[j]) * height[j]);
}
return area;
}
};
__________________________________________________________________________________________________
10748 kb
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if (matrix.empty() || matrix[0].empty()) return 0;
int n = matrix.size(), m = matrix[0].size();
vector<int> A(m, 0);
stack<int> st;
int ans = 0, pos = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
A[j] = matrix[i][j] == '1' ? A[j]+1 : 0;
while (!st.empty() && A[st.top()] > A[j]) {
pos = st.top(); st.pop();
ans = max(ans, A[pos] * (st.empty() ? j : j-st.top()-1));
}
st.push(j);
}
while (!st.empty()) {
pos = st.top(); st.pop();
ans = max(ans, A[pos] * (st.empty() ? m : m-st.top()-1));
}
}
return ans;
}
};
__________________________________________________________________________________________________