-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathget_minimun_from_stack.py
More file actions
39 lines (33 loc) · 895 Bytes
/
Copy pathget_minimun_from_stack.py
File metadata and controls
39 lines (33 loc) · 895 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
32
33
34
35
36
37
38
39
# use 2 stack 1. Original stack & 2. Supporting stack for Minimum stack
from stack import Stack
stack = Stack()
support_stack = Stack()
class MinStack:
def push(self,item):
if stack.is_empty():
stack.push(item)
support_stack.push(item)
else:
stack.push(item)
if (stack.peek() < support_stack.peek()):
support_stack.push(item)
def pop(self):
if not stack.is_empty():
if stack.peek() == support_stack.peek():
stack.pop()
support_stack.pop()
else:
stack.pop()
def get_min(self):
if not support_stack.is_empty():
return support_stack.peek()
s = MinStack()
s.push(10)
s.push(3)
s.push(4)
s.pop()
s.pop()
s.push(12)
s.push(8)
s.push(9)
print(stack.printStack(), support_stack.printStack())