Python (Solution) O(1) time for all functions


#1
class MinStack:
# @param x, an integer
def __init__(self):
    self.stackin = []
    self.minii = []
def push(self, x):
    if len(self.minii)==0 or self.minii[-1]>=x:
        self.minii.append(x)
    self.stackin.append(x)

# @return nothing
def pop(self):
    if len(self.stackin)>0:
        temp = self.stackin[-1]
        self.stackin.pop()
        if len(self.minii)>0 and self.minii[-1]==temp:
            self.minii.pop()
# @return an integer
def top(self):
    if len(self.stackin)>0:
        return self.stackin[-1]
    return -1

# @return an integer
def getMin(self):
    if len(self.minii)>0:
        return self.minii[-1]
    return -1