Simple c++ solution O(1) time complexity


#1

vector<pair<int,int>>vec;
MinStack::MinStack()
{
vec.clear();
}

void MinStack::push(int x)
{
if(vec.size()==0)
vec.emplace_back(x,x);
else
vec.emplace_back(x,min((vec.back().second),x));
}

void MinStack::pop()
{
if(vec.size()>0)
vec.pop_back();
}

int MinStack::top()
{
if(vec.size()==0)
return -1;
return (vec.back()).first;
}

int MinStack::getMin()
{
if(vec.size()==0)
return -1;
return (vec.back()).second;
}