LC 155最小栈Min Stack 中等
设计支持 push、pop、top 以及在常数时间内取最小值 getMin 的栈结构。
思路 辅助栈维护当前最小:新值 <= 辅助栈顶才压入,出栈值等于辅助栈顶时同步弹出;getMin 取其栈顶。
class MinStack: def __init__(self): self.st = [] self.min_st = [] def push(self, val: int) -> None: self.st.append(val) if not self.min_st or val <= self.min_st[-1]: self.min_st.append(val) def pop(self) -> None: if self.st.pop() == self.min_st[-1]: self.min_st.pop() def top(self) -> int: return self.st[-1] def getMin(self) -> int: return self.min_st[-1]