-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxStack716.py
More file actions
68 lines (57 loc) · 1.95 KB
/
Copy pathMaxStack716.py
File metadata and controls
68 lines (57 loc) · 1.95 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'''
716. MaxStack
Design a max stack data structure that supports the stack operations and supports finding the stack's maximum element.
Implement the MaxStack class:
MaxStack() Initializes the stack object.
void push(int x) Pushes element x onto the stack.
int pop() Removes the element on top of the stack and returns it.
int top() Gets the element on the top of the stack without removing it.
int peekMax() Retrieves the maximum element in the stack without removing it.
int popMax() Retrieves the maximum element in the stack and removes it.
If there is more than one maximum element, only remove the top-most one.
'''
class MaxStack:
def __init__(self):
self.stack = []
self.max_stack = []
def push(self, x):
self.stack.append(x)
if len(self.max_stack) == 0:
self.max_stack.append(x)
return
if self.max_stack[-1] > x:
self.max_stack.append(self.max_stack[-1])
else:
self.max_stack.append(x)
def pop(self):
if len(self.stack) != 0:
self.max_stack.pop(-1)
return self.stack.pop(-1)
def top(self):
return self.stack[-1]
def peekMax(self):
if len(self.max_stack) != 0:
return self.max_stack[-1]
def popMax(self):
val = self.peekMax()
curr = []
while self.top() != val:
curr.append(self.pop())
self.pop()
while len(curr) != 0:
self.push(curr.pop(-1))
return val
def test():
testStack = MaxStack()
testStack.push(12)
testStack.push(1)
testStack.push(2)
testStack.push(122)
testStack.push(21)
testStack.push(12)
print("Pushed: 12, 1, 2, 122, 21, 12")
print("Stack Pop:", testStack.pop())
print("Stack Top:", testStack.top())
print("Stack Peek Max:", testStack.peekMax())
print("Stack Pop Max:", testStack.popMax())
test()