# 155. Min Stack

[https://leetcode.com/problems/min-stack/](https://leetcode.com/problems/min-stack/)

## solution

* 维护每个数入栈时的最小值，即另一个栈min\_stack. 利用其特点，当前加入时的最小值为自己或之前更早的元素，保证pop时小的不会pop出去而无法更新

```python
class MinStack:
    def __init__(self):
        self.stack = []
        self.min_stack = []

    def push(self, val: int) -> None:
        self.stack.append(val)
        if not self.min_stack or val < self.min_stack[-1]:
            self.min_stack.append(val)
        else:
            self.min_stack.append(self.min_stack[-1])

    def pop(self) -> None:
        self.min_stack.pop()
        return self.stack.pop()

    def top(self) -> int:
        return self.stack[-1]

    def getMin(self) -> int:
        return self.min_stack[-1]
```

时间复杂度：O(1)\
空间复杂度：O(n)

## follow up

[\*716. Max Stack](https://leetcode.com/problems/max-stack/)

```python
```

[622. Design Circular Queue](https://leetcode.com/problems/design-circular-queue/description/)

```python
# 关键是记录head_index和count，从一段空间中确定时间的queue. 整个空间其他的还可以记录

```
