forked from rituparna-ui/hacktorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_rectangle.cpp
107 lines (102 loc) · 2.29 KB
/
max_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
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
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<"testcases\n";
int t;
cin>>t;
while(t--)
{
cout<<"size\n";
int size;
cin>>size;
int x[size];
for(int i=0;i<size;i++)
{
cin>>x[i];
}
stack<int>st;
//building right index array//
int right[size];
right[size-1]=size;
st.push(size);
for(int i=size-2;i>=0;i--)
{
if(x[i]<x[st.top()-1])
{
while(x[st.top()-1]<x[i] && !st.empty())
{
st.pop();
}
if(st.empty())
{
st.push(size);
right[i]=size;
}slid
else
{
st.push(st.top());
}
}
else
{
x[i]=size;
st.push(x[i]);
}
}
for(int i=0;i<size;i++)
{
cout<<right[i]<<" ";
}
cout<<endl;
//getting emptied stack//
while(!st.empty())
{
st.pop();
}
//building left index array//
int left[size];
left[0]=-1;
st.push(-1);
for(int i=1;i<size;i++)
{
if(x[i]<x[st.top()])
{
while(x[st.top()]<x[i] && !st.empty())
{
st.pop();
}
if(st.empty())
{
st.push(-1);
left[i]=size;
}
else
{
st.push(st.top());
}
}
else
{
x[i]=-1;
st.push(x[i]);
}
}
for(int i=0;i<size;i++)
{
cout<<left[i]<<" ";
}
//23987590328//
int john=0;
int wick=0;
for(int i=0;i<size;i++)
{
john=(abs(left[i]-right[i]))*x[i];
if(john>wick)
{
wick=john;
}
}
// cout<<wick<<" ";
}
}