-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMaximum Element.cpp
More file actions
46 lines (36 loc) · 810 Bytes
/
Maximum Element.cpp
File metadata and controls
46 lines (36 loc) · 810 Bytes
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
//Practice => Data Structures => Stacks => Maximum Element
https://www.hackerrank.com/challenges/maximum-element/problem
#include<bits/stdc++.h>
using namespace std;
int main() {
std::stack<int> st;
int n, x;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int q;
scanf("%d", &q);
switch (q)
{
case 1:
scanf("%d", &x);
if (st.empty()) {
st.push(x);
}
else {
st.push(max(x, st.top()));
}
break;
case 2:
if (!st.empty()) {
st.pop();
}
break;
case 3:
printf("%d\n", st.top());
break;
default:
break;
}
}
return 0;
}