-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildingStackWrongApproach.cpp
More file actions
executable file
·52 lines (51 loc) · 1.37 KB
/
Copy pathBuildingStackWrongApproach.cpp
File metadata and controls
executable file
·52 lines (51 loc) · 1.37 KB
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll n;
cin >> n;
vector< ll > input(n);
ll maximumValue = 0;
for(ll i = 0; i < n; i++)
{
cin >> input[i];
}
vector< ll > buildingStack;
for(ll i = 0; i < n; i++)
{
if((!buildingStack.empty() && input[buildingStack.back()] <= input[i]) || buildingStack.empty())
{
// cout << "Push Back" << i << endl;
buildingStack.push_back(i);
}
else
{
while(!buildingStack.empty() && input[buildingStack.back()] > input[i])
{
ll index = buildingStack.back();
ll value = (i - index) * input[index];
if(value > maximumValue)
{
maximumValue = value;
// cout << index << " " << maximumValue << endl;
}
buildingStack.pop_back();
}
buildingStack.push_back(i);
}
}
while(!buildingStack.empty())
{
ll index = buildingStack.back();
ll value = (n - index) * input[index];
if(value > maximumValue)
{
maximumValue = value;
// cout << index << " " << maximumValue << endl;
}
buildingStack.pop_back();
}
cout << maximumValue << endl;
return 0;
}