-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximal_Rectangle.cpp
More file actions
49 lines (49 loc) · 1.25 KB
/
Maximal_Rectangle.cpp
File metadata and controls
49 lines (49 loc) · 1.25 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
class Solution {
public:
int largestRectangleArea(vector<int>& A);
int maximalRectangle(vector<vector<char>>& matrix) {
int res = 0;
int n = matrix.size();
if(n==0)
return res;
int m = matrix[0].size();
if(m==0)
return res;
vector<int> heights(m,0);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(matrix[i][j]=='0')
heights[j] = 0;
else
heights[j] += 1;
}
res = max(res,largestRectangleArea(heights));
}
return res;
}
};
int Solution::largestRectangleArea(vector<int> &A) {
int n = A.size(),i;
stack<int> st;
int res = 0,top,area;
for(i=0; i<n; i++){
if(st.empty() || A[st.top()]<=A[i]){
st.push(i);
continue;
}
while(!st.empty() && A[i]<A[st.top()]){
top = st.top();
st.pop();
area = A[top]*(st.empty() ? i: i-1-st.top());
res = max(res,area);
}
st.push(i);
}
while(!st.empty()){
top = st.top();
st.pop();
area = A[top]*(st.empty() ? i: i-1-st.top());
res = max(res,area);
}
return res;
}