Skip to content

Instantly share code, notes, and snippets.

@jay3686
Last active August 17, 2016 17:56
Show Gist options
  • Save jay3686/f19a6830ec4af54c623c9a41413a064d to your computer and use it in GitHub Desktop.
Save jay3686/f19a6830ec4af54c623c9a41413a064d to your computer and use it in GitHub Desktop.
basic min stack implementation
class MinStack:
def __init__(self):
self.data = []
self.mins = []
# @param x, an integer
def push(self, x):
self.data.append(x)
if self.mins == [] or self.mins[-1] >= x:
self.mins.append(x)
# @return an integer
def pop(self):
if not self.data:
return -1
x = self.data.pop()
if self.mins and self.mins[-1] == x:
self.mins.pop()
return x
# @return an integer
def top(self):
if not self.data:
return -1
return self.data[-1]
# @return an integer
def getMin(self):
if not self.data:
return -1
return self.mins[-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment