-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMaximumElement.cpp
More file actions
45 lines (39 loc) · 1.06 KB
/
MaximumElement.cpp
File metadata and controls
45 lines (39 loc) · 1.06 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
//https://www.hackerrank.com/challenges/maximum-element/problem
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int N, i = 0;
cin >> N;
stack<long int> myStack, maxStack;
while(i < N){
int type, x;
cin >> type;
if(type == 1){
cin >> x;
// Push to stacks
myStack.push(x);
if(!maxStack.empty()){
if(x >= maxStack.top()){ maxStack.push(x); }
}else{
maxStack.push(x);
}
}else if(type == 2){
// delete element at top
if(!myStack.empty()){
if(myStack.top() == maxStack.top()) { maxStack.pop(); }
myStack.pop();
}
}else if(type == 3){
// print maximum
cout << maxStack.top() << endl;
}
i++;
}
return 0;
}