-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0085_Maximal_Rectangle.cpp
More file actions
46 lines (44 loc) · 1.34 KB
/
0085_Maximal_Rectangle.cpp
File metadata and controls
46 lines (44 loc) · 1.34 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
#pragma GCC optimize("Ofast","inline","ffast-math","unroll-loops","no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native","f16c")
auto init = []()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 'c';
}();
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int res = 0, n = heights.size();
stack<int> stk;
vector<int> left(n, -1);
vector<int> right(n, n);
for (int i = 0; i < n; ++i) {
while (!stk.empty() && heights[stk.top()] >= heights[i]) {
right[stk.top()] = i;
stk.pop();
}
if (!stk.empty()) left[i] = stk.top();
stk.push(i);
}
for (int i = 0; i < n; ++i)
res = max(res, heights[i] * (right[i] - left[i] - 1));
return res;
}
int maximalRectangle(vector<vector<char>>& matrix) {
int area=0;
int n = matrix[0].size();
vector<int> heights(n);
for (auto& rr : matrix) {
for (int j = 0; j < n; ++j) {
if (rr[j] == '1')
++heights[j];
else
heights[j] = 0;
}
area = max(area, largestRectangleArea(heights));
}
return area;
}
};