-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmaximum area in histogram.cpp
99 lines (78 loc) · 2.09 KB
/
maximum area in histogram.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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
//Function to find largest rectangular area possible in a given histogram.
long long getMaxArea(long long arr[], int n)
{
// Your code here
stack<pair<long long,int>> st;
vector<long long> vec1;
int k=-1;
for(int i=0;i<n;i++)
{
while(!st.empty() && st.top().first>=arr[i])
{st.pop();
}
if(st.empty())
{
vec1.push_back(k);
}
else
vec1.push_back(st.top().second);
st.push({arr[i],i});
}
stack<pair<long long,int>> st1;
vector<long long> vec2;
for(int i=n-1;i>=0;i--)
{
while(!st1.empty() && st1.top().first>=arr[i])
{st1.pop();
}
if(st1.empty())
{
vec2.push_back(n);
}
else
vec2.push_back(st1.top().second);
st1.push({arr[i],i});
}
vector<long long> v;
reverse(vec2.begin(),vec2.end());
for(int i=0;i<n;i++)
v.push_back(vec2[i]-vec1[i]-1);
vector<long long> final;
for(int i=0;i<n;i++)
{
final.push_back(v[i]*arr[i]);
}
long long mx=final[0];
for(int i=0;i<n;i++)
{ //cout<<vec2[i]<<" ";
if(final[i]>mx)
mx=final[i];
}
return mx;
}
};
//{ Driver Code Starts.
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;
}
// } Driver Code Ends