Easy C++ solution using auxiliary stack


#1
stack<int>parent,aux;
MinStack::MinStack() 
{
    while(!parent.empty())
    {
        parent.pop();
    }
    while(!aux.empty())
    {
        aux.pop();
    }
}

void MinStack::push(int x) 
{
    parent.push(x);
    if(aux.empty())
    {
        aux.push(x);
    }
    else
    {
        aux.push(min(aux.top(),x));
    }
}

void MinStack::pop() 
{
    if(!parent.empty())
    {
        parent.pop();
        aux.pop();
    }
}

int MinStack::top() 
{
    if(parent.empty())
    {
        return -1;
    }
    return parent.top();
}

int MinStack::getMin() 
{
    if(aux.empty())
    {
        return -1;
    }
    return aux.top();
}