Help me with this code!


#1

int min_ele;
stacks;
MinStack::MinStack() {
while(!s.empty())
s.pop();

min_ele=-1; 

}

void MinStack::push(int x) {
if(s.size()==0){
s.push(x);
min_ele=x;
}
else if(x>=s.top())
s.push(x);
else{
s.push(2*x-min_ele);
min_ele=x;
}
}

void MinStack::pop() {
if(s.size()==0)
return;
if(s.top()>=min_ele)
s.pop();
else{
min_ele=2*min_ele-s.top();
s.pop();

} 

}

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

if(s.top()>=min_ele)
 return s.top();
else
 return min_ele;

}

int MinStack::getMin() {
if(s.size()==0)
return -1;
else
return min_ele;
}

Can anyone help me with my code?
Its giving wrong output


#2

Add the condition for empty stack in the top function and also in the pop function
if(s.empty()) { return -1; }
I can’t write the full solution using this approach here. but will be writing it in the solution section