-
Notifications
You must be signed in to change notification settings - Fork 0
/
lc_0155_min_stack.py
62 lines (48 loc) · 1.71 KB
/
lc_0155_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
""" 155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Implement the MinStack class:
- MinStack() initializes the stack object.
- void push(int val) pushes the element val onto the stack.
- void pop() removes the element on the top of the stack.
- int top() gets the top element of the stack.
- int getMin() retrieves the minimum element in the stack.
"""
import unittest
class MinStack:
"""Single stack solution
Runtime: 40 ms, faster than 99.99% of Python3 online submissions for Min Stack.
Memory Usage: 17.9 MB, less than 93.47% of Python3 online submissions for Min Stack.
"""
def __init__(self):
self.deltas = []
self.min = None
def push(self, val: int) -> None:
if not self.deltas:
self.min = val
d = val - self.min
self.deltas.append(d)
if d < 0:
self.min += d
def pop(self) -> None:
d = self.deltas.pop()
if d < 0:
self.min -= d
return self.min + d
def top(self) -> int:
return self.min + max(0, self.deltas[-1])
def getMin(self) -> int:
return self.min
class MyTestCase(unittest.TestCase):
def setUp(self) -> None:
self.solution = MinStack()
def test_1(self):
self.solution.push(-2)
self.solution.push(3)
self.assertEqual(self.solution.top(), 3)
self.assertEqual(self.solution.getMin(), -2)
self.assertEqual(self.solution.pop(), 3)
self.assertEqual(self.solution.pop(), -2)
self.solution.push(5)
self.solution.push(-1)
self.assertEqual(self.solution.top(), -1)
self.assertEqual(self.solution.getMin(), -1)