# My Python solution

• ``````class MinStack:

def __init__(self):
self.q = []

# @param x, an integer
# @return an integer
def push(self, x):
curMin = self.getMin()
if curMin == None or x < curMin:
curMin = x
self.q.append((x, curMin));

# @return nothing
def pop(self):
self.q.pop()

# @return an integer
def top(self):
if len(self.q) == 0:
return None
else:
return self.q[len(self.q) - 1][0]

# @return an integer
def getMin(self):
if len(self.q) == 0:
return None
else:
return self.q[len(self.q) - 1][1]``````

• your idea of using curMin = self.getMin() to initialize curMin in first place is so smart. Thank u

• brilliant! I've almost forget tuple until I found this solution!

• You can just use self.q[-1] instead of self.q[len(self.q)-1] to get the last element.

• Your idea is great. But I don't know why I have a run time error...

• Brilliant!!Using current min to restore the Min value at that point,and set&get min become an o(1) function! Thanks very much!

• wow, very nice

• PEP8 - "Comparisons to singletons like None should always be done with is or is not , never the equality operators."

I would suggest replacing "currMin == None" with "currMiin is None"

• Here's my updated version:

``````class MinStack(object):

def __init__(self):
self.stack = []

def push(self, x):
self.stack.append((x, min(self.getMin(), x)))

def pop(self):
self.stack.pop()

def top(self):
if self.stack:
return self.stack[-1][0]

def getMin(self):
if self.stack:
return self.stack[-1][1]
return sys.maxint

``````

• This is such a beautiful solution! Thank you!!

• The use of a tuple to maintain the min and the latest value is ridiculously smart...

• @Evaxue me too,have you solved the problem?

• '''class MinStack(object):

``````def __init__(self):
"""
"""
self.q = []

def push(self, x):
"""
:type x: int
:rtype: void
"""
self.q.insert(0,x)

def pop(self):
"""
:rtype: void
"""
try:
self.q = self.q[1:]
except:
return "null"

def top(self):
"""
:rtype: int
"""
if self.q:
return self.q[0]

def getMin(self):
"""
:rtype: int
"""
try:
return min(self.q)
except:
return null
``````

'''
I wrote my code like this, while it cannot be accepted because of time limited. Will it be fine?

• @zhipinggao I think your problem is getMin(self) because every time calling it will also call the function min(). This complexity is too high.

• but for this question..should we only use .pop() to pop...
OR use sth like:

``````def pop(self):
if self.isEmpty():
raise EmptyStackError()
item = self.data[len(self.data) -1]
del self.data[len(self.data) -1]
return item
``````

• Anyone who can tell me, why do we need to make use of tuple here? Why can`t we make use of 1-dimensional list directly ?

• @Ivan_Ouyang Hey Ivan, if you try the way that you came up with, you will see TLE (Time Limit Exceeded) result.

• @charles8135 I was thinking how to keep track of the second min. Your solution of storing the current min is so smart!

• @Ivan_Ouyang It will take up too much time. Since every time you will need to re-search the rest of the stack to find the min again.

• @Seasean `del` in Python takes O(n) time while `pop` in Python only takes O(1).

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.