O(1) Solution python
\n# Approach\nCreate two seperate stacks, one is the main stack and the other is the minimum stack which holds the corresponding minimum value for each value in
\n# Approach\nCreate two seperate stacks, one is the main stack and the other is the minimum stack which holds the corresponding minimum value for each value in our main stack.\n\nWhen we want to push a value into our stack, we first check to see if the stack is empty. If it is, then we push the value in both our main stack and our minimum stack.\n\nIf the stack is not empty, we want to then compare the given value with the value at the top of our minimum stack. If the value if less than the top value of our minimum stack, we append the value in our minimum stack and our main stack. If the value is greater than the top of our minimum stack, we then push the value into our main stack but this time, we push the top of our minimum value in our minimum stack back into the minimum stack.\n\nThis way we can tell what value is the minimum of the stack without using any O(n) approaches.\n\n# Complexity\n- Time complexity: O(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass MinStack:\n\n    def __init__(self):\n        self.stack = []\n        self.minstack = []\n\n    def push(self, val: int) -> None:\n        if not self.stack:\n            self.stack.append(val)\n            self.minstack.append(val)\n        else:\n            if val < self.minstack[-1]:\n                self.minstack.append(val)\n                self.stack.append(val)\n            else:\n                self.stack.append(val)\n                self.minstack.append(self.minstack[-1])\n\n\n    def pop(self) -> None:\n        self.stack.pop()\n        self.minstack.pop()\n\n    def top(self) -> int:\n        return self.stack[-1]\n\n    def getMin(self) -> int:\n        return self.minstack[-1]\n\n\n# Your MinStack object will be instantiated and called as such:\n# obj = MinStack()\n# obj.push(val)\n# obj.pop()\n# param_3 = obj.top()\n# param_4 = obj.getMin()\n```
['Python3']