Created
May 1, 2018 19:15
-
-
Save cbzehner/208625daa5899874bec39d6fe7f82200 to your computer and use it in GitHub Desktop.
Largest Item In Stack
This file contains hidden or 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 MaxStack(Stack): | |
def __init__(self): | |
Stack.__init__() | |
self.max = [] | |
def push(self, item): | |
self.items.append(item) | |
if item > self.max): | |
self.max.push(item) | |
def pop(self): | |
item = self.items.pop() | |
if item == self.max.peek(): | |
return self.max.pop() | |
return item | |
def get_max(self): | |
return self.max.peek() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment