Using 2 stacks in O(1) time


#1

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

void MinStack::push(int x) {
s.push(x);
if(a.empty()!=0 || x<=a.top()) a.push(x);
return ;
}

void MinStack::pop() {
if(s.empty()==0){
if(s.top()==a.top()) a.pop();
s.pop();
}
return;
}

int MinStack::top() {
if(s.empty()==0) return s.top();
else return -1;
}

int MinStack::getMin() {
if(a.empty()==0) return a.top();
else return -1;
}

ment body goes here.