-
Notifications
You must be signed in to change notification settings - Fork 108
/
solution.cpp
31 lines (29 loc) · 730 Bytes
/
solution.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
#include<bits/stdc++.h> //This header file imports everything we need
using namespace std ;
int main()
{
/*In this problem we will not only store the value but also the max value from last to that paricular value*/
stack <pair<int,int> > stk; // We use pair to store {new_element,new_max}
int N ;
cin >> N ;
for(int i=1; i<=N; i++)
{
int option ;
cin >> option ;
if(option == 1)
{
int new_element ;
cin >> new_element ;
int new_max = stk.empty() ? new_element : max(new_element,stk.top().second) ;
stk.push({new_element, new_max}) ;
}
else if(option == 2)
{
stk.pop() ;
}
else
{
cout << stk.top().second << "\n" ; // Printing the max element
}
}
}