Last active
August 17, 2016 17:56
-
-
Save jay3686/f19a6830ec4af54c623c9a41413a064d to your computer and use it in GitHub Desktop.
basic min stack implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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