File tree 1 file changed +54
-0
lines changed
1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ long long maxArea (long long a[],long long n){
5
+ stack<long long > s;
6
+ long long max_area=0 ;
7
+ long long area_with_top;
8
+ long long top;
9
+
10
+ long long i=0 ;
11
+ while (i<n){
12
+ if (s.empty () || a[s.top ()]<=a[i]) // check if curr is > than curr_top
13
+ s.push (i++); // i++ because starting from 0
14
+
15
+ else {
16
+ top=s.top ();
17
+ s.pop ();
18
+ area_with_top=a[top]*(s.empty () ? i: i-s.top () -1 );
19
+
20
+ if (max_area<area_with_top)
21
+ max_area=area_with_top;
22
+
23
+ }
24
+ }
25
+
26
+
27
+ while (!s.empty ()){
28
+ top=s.top ();
29
+ s.pop ();
30
+ area_with_top=a[top]*(s.empty () ? i: i-s.top () -1 );
31
+
32
+ if (max_area<area_with_top)
33
+ max_area=area_with_top;
34
+
35
+ }
36
+ return max_area;
37
+ }
38
+
39
+ int main ()
40
+ {
41
+ // code
42
+ long long T;
43
+ cin>>T;
44
+ while (T--)
45
+ {
46
+ long long n;cin>>n;
47
+ long long arr[n];
48
+ for (long long i=0 ;i<n;i++)cin>>arr[i];
49
+
50
+ long long res=maxArea (arr,n);
51
+ cout<<res<<endl;
52
+ }
53
+ return 0 ;
54
+ }
You can’t perform that action at this time.
0 commit comments