-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProblem#43#746.py
More file actions
31 lines (28 loc) · 1019 Bytes
/
Problem#43#746.py
File metadata and controls
31 lines (28 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Implement a stack that has the following methods:
push(val), which pushes an element onto the stack
pop(), which pops off and returns the topmost element of the stack. If there are no elements in the stack,
then it should throw an error or return null.
max(), which returns the maximum value in the stack currently. If there are no elements in the stack,
then it should throw an error or return null.
Each method should run in constant time.
"""
class maxStack:
def __init__(self):
self.stack = []
self.maxS = []
def push(self, data):
self.stack.append(data)
if not self.maxS or self.maxS[-1] <= data:
self.maxS.append(data)
def pop(self):
if not self.stack:
return None
popData = self.stack.pop()
if self.maxS[-1] == popData:
self.maxS.pop()
return popData
def max(self):
if not self.maxS:
return None
return self.maxS[-1]