forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path85-Maximal-Rectangle.cpp
66 lines (55 loc) · 1.49 KB
/
85-Maximal-Rectangle.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
class Solution {
public:
/**
* @param matrix a boolean 2D matrix
* @return an integer
*/
int maximalRectangle(vector<vector<bool> > &matrix)
{
int M=matrix.size();
if (M==0) return 0;
int N=matrix[0].size();
auto q= vector<int>(N,0);
int result = 0;
for (int i=0; i<M; i++)
{
for (int j=0; j<N; j++)
{
if (matrix[i][j]==0)
q[j]=0;
else
q[j]=q[j]+1;
}
result = max(result, helper(q));
}
return result;
}
int helper(vector<int>height)
{
if (height.size()==0) return 0;
if (height.size()==1) return height[0];
stack<int>s;
height.push_back(0);
height.insert(height.begin(),0);
int result=0;
for (int i=0; i<height.size(); i++)
{
if (s.empty() || height[i]>=height[s.top()])
{
s.push(i);
continue;
}
if (height[i]<height[s.top()])
{
while (!s.empty() && height[i]<height[s.top()])
{
int H = height[s.top()];
s.pop();
result = max(result, H*(i-s.top()-1));
}
s.push(i);
}
}
return result;
}
};