<jupyter_start><jupyter_text>Min Stack（最小栈）[@mjd507](https://github.com/mjd507)![Logo](DP-69.png) Design a simple stack that supports push, pop, top, and retrieving the minimum element in constant time.- `push(x)` - Push element x onto stack.- `pop()` - Removes the element on top of the stack.- `top()` - Get the top element.- `getMin()` - Retrieve the minimum element in the stack.**Note: be sure that `pop()` and `top()` handle being called on an empty stack.** &emsp;&emsp;设计一个简单的栈，支持压入、弹出、取栈顶和在$O(1)$时间内获取栈内最小元素。- `push(x)` - 将元素$x$压栈- `pop()` - 使栈顶元素出栈- `top()` - 取栈顶元素- `getMin()` - 获取栈内最小元素&emsp;&emsp;**注意：确保`pop()`和`top()`能够解决在栈空时被调用的情况。**<jupyter_code>class MinStack(object):
    def __init__(self):
        self.stack = []

    def push(self, x: int):
        self.stack.append({
            'value': x,
            'min': min(x, self.getMin())
        })

    def pop(self) -> int:
        if self.stack == []:
            return None
        else:
            return self.stack.pop()['value']

    def top(self) -> int:
        if self.stack == []:
            return None
        else:
            return self.stack[-1]['value']

    def getMin(self) -> int:
        if self.stack == []:
            return float('inf')
        else:
            return self.stack[-1]['min']
x = MinStack()
x.push(-2)
x.push(0)
x.push(-3)
print(x.getMin())
x.pop()
print(x.top())
print(x.getMin())<jupyter_output>-3
0
-2
{'fraction_non_alphanumeric': 0.18406205923836388, 'fraction_numerical': 0.011988716502115656, 'mean_word_length': 2.6198979591836733, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '98316', 'license': 'MIT', 'n_tokens_mistral': 556, 'n_tokens_neox': 560, 'n_words': 124, 'path': 'March/Week10/69.ipynb', 'repo_name': 'Dragon1573/Daily-Problem', 'size': '1418'}