Need help with question min stack

class MinStack {
public:
    /** initialize your data structure here. */
    vector<int>  stack;
    vector<int> min_stack;
    int smallest;
    
    MinStack() {
        smallest = INT_MAX;
        stack = {};
        min_stack = {};
    }
    
    void push(int val){
        
        stack.push_back(val);
        if(!min_stack.empty()){
            if(val <= min_stack.back()){
                min_stack.push_back(val);
            }
        }
        else{
           min_stack.push_back(val); 
        }
        
    }
    
    void pop() {
        
        int stackpop = stack.back();
        stack.pop_back();
        if(stackpop == min_stack.back()){
            if(!stack.empty()){
                min_stack.pop_back();
            }
        }
    }
    
    int top() {     
        return stack.back(); 
        
    }
    
    int getMin() {
        
        return min_stack.back();
        
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(val);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */
 

this fails on some of the test cases and for the life of me i cannot figure out why. Ive wrote the same logic for python and it works, can someone please tell me why this fails thanks in advance

Comments (0)