-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinStack.py
More file actions
41 lines (34 loc) · 919 Bytes
/
MinStack.py
File metadata and controls
41 lines (34 loc) · 919 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
40
41
from types import *
import heapq
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
print('asdasdfadf')
self.stack = []
self.heap = []
def resetHeap(self):
self.heap = self.stack.copy()
heapq.heapify(self.heap)
def push(self, x: int) -> None:
self.stack.append(x)
self.resetHeap()
def pop(self) -> None:
self.stack.pop()
self.resetHeap()
def top(self) -> int:
return self.stack[len(self.stack) - 1]
def getMin(self) -> int:
return self.heap[0]
if __name__ == "__main__":
import doctest
doctest.testmod()
# MinStack minStack = new MinStack();
# minStack.push(-2);
# minStack.push(0);
# minStack.push(-3);
# minStack.getMin(); --> Returns -3.
# minStack.pop();
# minStack.top(); --> Returns 0.
# minStack.getMin(); --> Returns -2.