-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS-1.4.StackWithMaximum.cpp
46 lines (42 loc) · 1.02 KB
/
DS-1.4.StackWithMaximum.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
#include<bits/stdc++.h>
using namespace std;
int main() {
int num_queries = 0;
cin >> num_queries;
string query;
string value;
stack < int > s;
int maxx;
for (int i = 0; i < num_queries; ++i) {
cin >> query;
if (query == "push") {
cin >> value;
int val=stoi(value);
if(s.empty())
maxx=val;
else if(!s.empty()&&val>maxx)
{
val = maxx-val;
maxx=maxx-val;
}
s.push(val);
}
else if (query == "pop") {
int top = s.top();
s.pop();
if(s.empty())
{
maxx = top;;
}
else if(!s.empty()&&top<0)
maxx = maxx+top;
}
else if (query == "max") {
cout << maxx << "\n";
}
else {
assert(0);
}
}
return 0;
}