We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here's my solution that has checks to avoid Segmentation Errors
#include<cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>#include<stack>usingnamespacestd;intmain(){intN,i=0;cin>>N;stack<longint>myStack,maxStack;while(i<N){inttype,x;cin>>type;if(type==1){cin>>x;// Push to stacksmyStack.push(x);if(!maxStack.empty()){if(x>=maxStack.top()){maxStack.push(x);}}else{maxStack.push(x);}}elseif(type==2){// delete element at topif(!myStack.empty()){if(myStack.top()==maxStack.top()){maxStack.pop();}myStack.pop();}}elseif(type==3){// print maximumcout<<maxStack.top()<<endl;}i++;}return0;}
Maximum Element
You are viewing a single comment's thread. Return to all comments →
Here's my solution that has checks to avoid Segmentation Errors