[Python] two stacks solution, explained
Here we need to maintain two stacks: one is original one and another is min_stack, where we keep elements in descending order. So, when we pop element from main
Here we need to maintain two stacks: one is original one and another is min_stack, where we keep elements in descending order. So, when we pop element from main stack, we also check if it is also on the top of min stack and if it is, delete it. To get `top` and `getMin` just look at the top of main and min stack.\n\n#### Complexity\nIt is `O(1)` for all operations for time and `O(n)` for space after `n` queries.\n\n#### Code\n```python\nclass MinStack:\n    def __init__(self):\n        self.stack_main = []\n        self.stack_min = []\n        \n    def push(self, x):\n        self.stack_main.append(x)\n        if not self.stack_min or self.stack_min[-1] >= x:\n            self.stack_min.append(x)\n\n    def pop(self):\n        last = self.stack_main.pop()\n        if last == self.stack_min[-1]:\n            self.stack_min.pop()\n\n    def top(self):\n        return self.stack_main[-1]\n\n    def getMin(self):\n        return self.stack_min[-1]\n```\n\nIf you have any questoins, feel free to ask. If you like the solution and explanation, please **upvote!**
['Stack']