GETTING RUN TIME ERROR < Please help


#1

stack s;
int mn;

MinStack::MinStack() {
while(!s.empty()){
s.pop();
}
}

void MinStack::push(int x) {
if(s.empty()){
mn=x;
s.push(x);
return;
}
if(x<s.top()){
s.push(2*x-mn);
mn=x;
}
else s.push(x);
}

void MinStack::pop() {
if(s.empty()) return ;
if(s.top()<mn){
int prevmin = 2*mn - s.top();
mn= prevmin;
s.pop();
}
else s.pop();
}

int MinStack::top() {
if(s.empty()) return -1;
if(mn<s.top()) return mn;
return s.top();
}

int MinStack::getMin() {
if(s.empty()) return -1;
return mn;
}


#2

Bro we have to create our own stack class ,then why are you using STL stack in your solution.


#3

I implemented a stack using a large array 2000000 size maybe. It was accepted. Try doing that. That may avoid runtime error.