Best O(1) Solution
Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass MinStack {\n    stack <long long> st;\n    long long minEle;\npublic:\n    
# Complexity\n- Time complexity:\n$$O(1)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass MinStack {\n    stack <long long> st;\n    long long minEle;\npublic:\n    MinStack() {\n        \n    }\n    \n    void push(int val) {\n        if (st.empty()) {\n            st.push(val);\n            minEle = val;\n        } else if (val < minEle){\n            st.push((long long) 2 * val - minEle);\n            minEle = val;\n        } else {\n            st.push(val);\n        }\n    }\n    \n    void pop() {\n        long long popped = st.top();\n        st.pop();\n        if (popped < minEle)\n            minEle = 2 * minEle - popped;\n    }\n    \n    int top() {\n        return (st.top() < minEle) ? minEle : st.top();\n    }\n    \n    int getMin() {\n        return minEle;\n    }\n};\n\n/**\n * Your MinStack object will be instantiated and called as such:\n * MinStack* obj = new MinStack();\n * obj->push(val);\n * obj->pop();\n * int param_3 = obj->top();\n * int param_4 = obj->getMin();\n */\n```
['C++']