Solution
-
When an element is pushed, it doesn’t have to update the
min
value. -
Only when the new min is pushed/popped,
min
get updated. -
We can bind
min
with the value, such that when an element is pushed/popped, we can know if it affects themin
. -
Store the information in a list of tuple
(x, min)
. Thus the last element always contains themin
.
def push(self, x):
"""
:type x: int
:rtype: None
"""
if not self.stack:
self.stack.append((x, x))
else:
self.stack.append((x, min(self.stack[-1][1], x)))