-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path85.cpp
More file actions
68 lines (64 loc) · 1.97 KB
/
85.cpp
File metadata and controls
68 lines (64 loc) · 1.97 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
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if(matrix.empty() || matrix[0].empty())
return 0;
int m = matrix.size(), n = matrix[0].size(),top,res=0,curr;
vector<int> heights(n+1,0);
for(int i =0; i<m; i++){
for(int j=0; j<n; j++){
if(matrix[i][j]=='1')
heights[j]++;
else
heights[j] = 0;
}
stack<int> st;
st.push(-1);
for(int j=0; j<=n; j++){
while(st.top()!=-1 && heights[st.top()]>heights[j]){
top = st.top();st.pop();
curr = heights[top]*(j-st.top()-1);
if(res<curr)
res = curr;
}
st.push(j);
}
}
return res;
}
};
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if(matrix.empty() || matrix[0].empty())
return 0;
int m = matrix.size(), n = matrix[0].size();
int res = 0;
vector<int> height(n,0),left(n,0),right(n,n);
for(int i=0; i<m; i++){
int curr_left = 0, curr_right = n;
for(int j=0; j<n; j++){
if(matrix[i][j]=='1'){
height[j]++;
left[j] = max(left[j],curr_left);
}
else{
height[j] = 0;
left[j] = 0;
curr_left = j+1;
}
int jr = n-j-1;
if(matrix[i][jr]=='1')
right[jr] = min(right[jr],curr_right);
else{
right[jr] = n;
curr_right = jr;
}
}
for(int j=0; j<n; j++){
res = max(res, (right[j]-left[j])*height[j]);
}
}
return res;
}
};