-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q02_stack_min.py
39 lines (30 loc) · 946 Bytes
/
Q02_stack_min.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
import unittest
class MinStack:
def __init__(self):
self.stack = []
self.min_stack = []
def push(self, val):
if val <= self.peek_min():
self.min_stack.append(val)
self.stack.append(val)
def peek(self):
return self.stack[-1]
def peek_min(self):
if not self.min_stack:
return float("inf")
return self.min_stack[-1]
def pop(self):
value = self.stack.pop()
if value == self.peek_min():
self.min_stack.pop()
return value
class Test(unittest.TestCase):
def test_min_stack(self):
min_stack = MinStack()
min_stack.push(2)
self.assertEqual(2, min_stack.peek())
self.assertEqual(2, min_stack.peek_min())
min_stack.push(1)
self.assertEqual(1, min_stack.peek_min())
self.assertEqual(1, min_stack.pop())
self.assertEqual(2, min_stack.peek_min())