C++/Java/Python/JavaScript |✅🚀Simple Solution with O(1) time complexity for each function as asked.
Intuition\nThe intuition behind this approach is to store the minimum value encountered so far along with each element in the stack. By doing so, the minimum va
# Intuition\nThe intuition behind this approach is to store the minimum value encountered so far along with each element in the stack. By doing so, the minimum value can be retrieved in constant time by accessing the second element of the pair at the top of the stack.\n\n# Approach\n1. The class `MinStack` has a member variable `st`, which is a stack of pairs of integers. Each pair consists of the element and the minimum element encountered so far.\n\n2. The constructor `MinStack()` is empty and does not perform any operations.\n\n3. The `push` function takes an integer `val` as input and pushes it onto the stack. If the stack is empty, it sets the minimum value encountered as `val` and pushes the pair `{val, val}` onto the stack. If the stack is not empty, it calculates the new minimum value by taking the minimum of the current minimum value (`st.top().second`) and `val`, and pushes the pair `{val, mn}` onto the stack, where `mn` is the new minimum value.\n\n4. The `pop` function removes the top element from the stack if it is not empty.\n\n5. The `top` function returns the element at the top of the stack if it is not empty. If the stack is empty, it returns 0 as a default value.\n\n6. The `getMin` function returns the minimum element encountered so far, which is stored as the second element of the pair at the top of the stack.\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- The time complexity of the `push`, `pop`, `top`, and `getMin` functions is O(1) since all operations are performed directly on the stack and do not depend on the stack\'s size.\n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- The space complexity of the code is O(n), where n is the number of elements pushed onto the stack. This is because the stack `st` stores pairs of integers for each element, resulting in additional space proportional to the number of elements in the stack.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# C++\n```\nclass MinStack {\npublic:\n    stack<pair<int,int>> st;\n    MinStack() {\n        \n    }\n    \n    void push(int val) {\n        if(st.empty()){\n            int mn = val;\n            st.push({val,val});\n        }\n        else{\n            int mn = min(st.top().second,val);\n            st.push({val,mn});\n        }\n        \n    }\n    \n    void pop() {\n        if(!st.empty()){\n            st.pop();\n        }\n    }\n    \n    int top() {\n        if(st.empty()){\n            return 0;\n        }\n        return st.top().first;\n    }\n    \n    int getMin() {\n        return st.top().second;\n    }\n};\n```\n# Java\n```\nclass MinStack {\n    private Stack<Pair<Integer, Integer>> stack;\n\n    public MinStack() {\n        stack = new Stack<>();\n    }\n\n    public void push(int val) {\n        if (stack.isEmpty()) {\n            stack.push(new Pair<>(val, val));\n        } else {\n            int min = Math.min(stack.peek().getValue(), val);\n            stack.push(new Pair<>(val, min));\n        }\n    }\n\n    public void pop() {\n        if (!stack.isEmpty()) {\n            stack.pop();\n        }\n    }\n\n    public int top() {\n        if (stack.isEmpty()) {\n            return 0;\n        }\n        return stack.peek().getKey();\n    }\n\n    public int getMin() {\n        if (stack.isEmpty()) {\n            return 0;\n        }\n        return stack.peek().getValue();\n    }\n}\n\n```\n# Python\n```\nclass MinStack:\n    def __init__(self):\n        self.stack = []\n\n    def push(self, val):\n        if not self.stack:\n            self.stack.append((val, val))\n        else:\n            mn = min(self.stack[-1][1], val)\n            self.stack.append((val, mn))\n\n    def pop(self):\n        if self.stack:\n            self.stack.pop()\n\n    def top(self):\n        if self.stack:\n            return self.stack[-1][0]\n        return 0\n\n    def getMin(self):\n        if self.stack:\n            return self.stack[-1][1]\n        return 0\n\n```\n# JavaScript\n```\nvar MinStack = function() {\n  this.stack = [];\n};\n\nMinStack.prototype.push = function(val) {\n  if (this.stack.length === 0) {\n    this.stack.push({ val: val, min: val });\n  } else {\n    var min = Math.min(this.stack[this.stack.length - 1].min, val);\n    this.stack.push({ val: val, min: min });\n  }\n};\n\nMinStack.prototype.pop = function() {\n  if (this.stack.length > 0) {\n    this.stack.pop();\n  }\n};\n\nMinStack.prototype.top = function() {\n  if (this.stack.length > 0) {\n    return this.stack[this.stack.length - 1].val;\n  }\n  return null;\n};\n\nMinStack.prototype.getMin = function() {\n  if (this.stack.length > 0) {\n    return this.stack[this.stack.length - 1].min;\n  }\n  return null;\n};\n```
['Python', 'C++', 'Java', 'JavaScript']