-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximalRectangle.cpp
More file actions
43 lines (39 loc) · 1.08 KB
/
Copy pathMaximalRectangle.cpp
File metadata and controls
43 lines (39 loc) · 1.08 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
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if (matrix.empty()) return 0;
vector<int> hists;
hists.resize(matrix[0].size());
int max_area = 0;
for (int i = 0; i < matrix.size(); i++)
{
for (int j = 0; j < matrix[0].size(); j++)
{
hists[j] = matrix[i][j] == '0' ? 0 : (1 + hists[j]);
}
max_area = max(max_area, largestRectangleArea(hists));
}
return max_area;
}
int largestRectangleArea(vector<int>& heights) {
stack<int> stk;
int max_area = 0;
int i = 0;
vector<int> h = heights;
h.push_back(0);
while (i < h.size())
{
if (stk.empty() || h[stk.top()] <= h[i])
{
stk.push(i++);
}
else
{
int j = stk.top();
stk.pop();
max_area = max(max_area, h[j] * (stk.empty() ? i : i - stk.top() - 1));
}
}
return max_area;
}
};