-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_stack.py
73 lines (60 loc) · 1.77 KB
/
min_stack.py
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
69
70
71
72
73
class Node:
def __init__(self):
self.value = None
self.next = None
self.occurances = 1
class Stack:
def __init__(self):
self.stack = None
def push(self, val: int) -> None:
old = self.stack
self.stack = Node()
self.stack.value = val
self.stack.occurances = 1
self.stack.next = old
def pop(self) -> None:
if self.stack is not None:
self.stack = self.stack.next
def top(self):
if self.stack is not None:
return self.stack.value
return None
class MinStack:
def __init__(self):
self.stack = Stack()
self.min_stack = Stack()
def push(self, val: int) -> None:
self.stack.push(val)
top_min = self.min_stack.top()
if top_min is not None:
if val < top_min:
self.min_stack.push(val)
elif val == top_min:
self.min_stack.stack.occurances += 1
else:
self.min_stack.push(val)
def pop(self) -> None:
value_to_delete = self.stack.top()
self.stack.pop()
top_stack = self.stack.top()
if top_stack is None:
self.min_stack.pop()
else:
top_min = self.min_stack.top()
if value_to_delete == top_min:
if self.min_stack.stack.occurances == 1:
self.min_stack.pop()
else:
self.min_stack.stack.occurances -= 1
def top(self):
return self.stack.top()
def getMin(self):
return self.min_stack.top()
if __name__ == '__main__':
minStack = MinStack()
minStack.push(0)
minStack.push(1)
minStack.push(0)
print(minStack.getMin())
minStack.pop()
print(minStack.getMin())