forked from sarthakd999/Hacktoberfest2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxRectangularArea.cpp
124 lines (108 loc) · 2.95 KB
/
MaxRectangularArea.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <bits/stdc++.h>
using namespace std;
//Time Complexity O(n), gave segmentation fault in gfg
long long getMaxArea(long long arr[], int n)
{
//left subarray
vector <long long > left; //stores index of NSL(Next greater to left)
stack <pair<long long , int>> s; //pair of NSL and index of NSL
int pseudoindex=-1;
for(int i=0;i<n;i++)
{
if(s.empty())
left.push_back(pseudoindex);
else if(!s.empty() && s.top().first<arr[i])
left.push_back(s.top().second);
else if(!s.empty() && s.top().first>=arr[i])
{
while(!s.empty() && s.top().first>=arr[i])
s.pop();
if(s.empty())
left.push_back(pseudoindex);
else
left.push_back(s.top().second);
}
s.push({arr[i],i});
}
//right subarray
vector <long long > right; //stores index of NSR
stack <pair<long long , int>> st; //pair of NSR and index of NSR
pseudoindex=n;
for( int i=n-1;i>=0;i--)
{
if(st.empty())
right.push_back(pseudoindex);
else if(!st.empty() && st.top().first<arr[i])
right.push_back(st.top().second);
else if(!st.empty() && st.top().first>=arr[i])
{
while(!st.empty() && st.top().first>=arr[i])
st.pop();
if(st.empty())
right.push_back(pseudoindex);
else
right.push_back(st.top().second);
}
st.push({arr[i],i});
}
reverse(right.begin(),right.end());
long long width[n],area[n],maxArea=INT_MIN;
for(int i=0;i<n;i++)
{
width[i]=right[i]-left[i]-1;
area[i]=width[i]*arr[i];
maxArea=max(maxArea,area[i]);
}
return maxArea;
}
int main()
{
long long t;
cin>>t;
while(t--)
{
int n;
cin>>n;
long long arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
Solution ob;
cout<<ob.getMaxArea(arr, n)<<endl;
}
return 0;
}
//Another approach, passed gfg
/*long long getMaxArea(long long arr[], int n)
{
stack<int> st;
int i;
long long area=INT_MIN, maxx= INT_MIN;
int temp;
for( i=0; i<n; i++)
{
while( !st.empty() && arr[st.top()] > arr[i]){
temp= st.top();
st.pop();
if(st.empty()){
area= arr[temp]* i; //i is rightmost smaller element's index
}
else{
area= arr[temp]* (i-st.top()-1); //st.top() is leftmost smaller element's index
}
maxx= max(area, maxx);
}
st.push(i);
}
while(!st.empty()){
temp= st.top();
st.pop();
if(st.empty()){
area= arr[temp]* i;
}
else{
area= arr[temp]* (i-st.top()-1);
}
maxx= max(area, maxx);
}
return maxx;
}*/