-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinStack.py
47 lines (36 loc) · 883 Bytes
/
MinStack.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
from math import inf
class MinStack:
def __init__(self):
self.stack = []
def min(self):
minStack = []
minValue = inf
for i in range(0, len(self.stack)): # 5, 4, 3, 2, 1
value = self.stack.pop() # 1
if value < minValue: #
minValue = value
minStack.append(value)
self.stack = minStack[::-1]
return minValue
def push(self, value):
self.stack.append(value)
def pop(self):
return self.stack.pop()
def peek(self):
return self.stack[len(self.stack) - 1]
def size(self):
return len(self.stack)
def main():
s = MinStack()
s.push(5)
s.push(4)
s.push(3)
s.push(2)
s.push(1)
print(s.min())
s.pop()
print(s.min())
print(s.peek())
print(s.size())
if __name__ == '__main__':
main()