Help me find error in my code


#1

stack <pair<long , long>> s;
long min_tillnow = INT_MAX;
MinStack::MinStack() {
while(s.empty()==0){
s.pop();
}
}

void MinStack::push(int x) {
if(s.empty()){
min_tillnow=INT_MAX;
}
if(min_tillnow>x){
min_tillnow = x;
}
s.push(make_pair(x,min_tillnow));
}

void MinStack::pop() {
if(s.empty());
else
s.pop();
}

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

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

}


#2

Update min_tillnow variable when the popped element is equal to min_tillnow in pop() function.

void MinStack::pop() {
    if(s.empty());
    else{
        auto tp = s.top().second;
        s.pop();
        if(!s.empty() && tp==min_tillnow){
            min_tillnow = s.top().second;
        }
    }
}

Your code gives wrong answer on cases like this:
Push, Push, Push, Pop, Push, GetMin
-10, 14, -20, NO OUTPUT, 10, -10
For last query GetMin as per your code, it will give -20 which is incorrect because the popped element is same as min_tillnow .