-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain (5).cpp
84 lines (75 loc) · 1.65 KB
/
main (5).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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>
using namespace std;
#define MAX 1000
int maxArea(int M[MAX][MAX],int n,int m);
int main()
{
int T;
cin>>T;
int M[MAX][MAX];
while(T--)
{
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>M[i][j];
}
}
cout<<maxArea(M,n,m)<<endl;
}
}
/*This is a function problem.You only need to complete the function given below*/
/*You are required to complete this method*/
int getAreaForARow(int row[], int n)
{
stack<int> s;
int max_area= INT_MIN;
int area_from_top;
int tp;
int i =0;
while(i<n)
{
if(s.empty() || row[s.top()]<=row[i])
s.push(i++);
else
{
tp = s.top();
s.pop();
area_from_top = row[tp]*(s.empty() ? i : i-s.top()-1);
if(area_from_top>max_area)
max_area = area_from_top;
}
}
while(!s.empty())
{
tp = s.top();
s.pop();
area_from_top = row[tp]*(s.empty() ? i : i-s.top()-1);
if(area_from_top>max_area)
max_area = area_from_top;
}
return max_area;
}
int maxArea(int arr[MAX][MAX],int n,int m)
{
//Your code here
int row[m];
int max_area = INT_MIN;
for(int i =0;i<m;i++)
row[i] = 0;
for(int i =0;i<n;i++)
{
for(int j =0;j<m;j++)
{
if(arr[i][j] == 0)
row[j] =0;
else
row[j]+=1;
}
max_area = max(getAreaForARow(row, m), max_area);
}
return max_area;
}