---
# System prepended metadata

title: 155-Min Stack
tags: [Easy]

---

# 155-Min Stack
###### tags: `Easy`
## Question
https://leetcode.com/problems/min-stack/
## Key
## Reference
https://medium.com/@ginaaiusing/leetcode-155-min-stack-c-1742952662dd
## Solution
```cpp=
class MinStack {
public:
    /** initialize your data structure here. */
    MinStack() {}      
        
    void push(int x) {
        A.push(x);
        if(B.empty()||x<=B.top()) 
            B.push(x);                   
    }
    
    void pop() {
        if(A.top()==B.top())
            B.pop();
        A.pop();
    }
    
    int top() {
        return A.top();               
    }
    
    int getMin() {
        return B.top();       
    }
private:
    stack<int> A,B;
};
```