-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximum_size_rectangle.cpp
45 lines (38 loc) · 1.21 KB
/
maximum_size_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
int maximalAreaOfSubMatrixOfAll1(vector<vector<int>> &arr, int n, int m){
for(int i=1;i<n;i++){
for(int j=0;j<m;j++){
if(arr[i][j]==0){
arr[i][j]=0;
}else{
arr[i][j]+=arr[i-1][j];
}
}
}long long ma=-1;
vector<long long>prevsmall(m,0);
vector<long long>nextsmall(m,0);
stack<pair<long long,long long>>sq;
for(int i=0;i<n;i++){
int j=0;
//previous small;
while(j<m){
while(!sq.empty() && arr[i][j]<=sq.top().first)sq.pop();
if(sq.empty())prevsmall[j]=-1;
else prevsmall[j]=sq.top().second;
sq.push({arr[i][j],j});
j++;
} while(!sq.empty())sq.pop();
//next small
j=m-1;
while(j>=0){
while(!sq.empty() && arr[i][j]<=sq.top().first)sq.pop();
if(sq.empty())nextsmall[j]=m;
else nextsmall[j]=sq.top().second;
sq.push({arr[i][j],j});
j--;
} while(!sq.empty())sq.pop();
for(long long j=0;j<m;j++){
ma=max(ma,(nextsmall[j]-prevsmall[j]-1)*arr[i][j]);
}
}
return ma;
}