-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmin_stack.py
More file actions
32 lines (26 loc) · 806 Bytes
/
min_stack.py
File metadata and controls
32 lines (26 loc) · 806 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
class Node:
def __init__(self, val, min, previous):
self.val = val
self.next = next
self.min = min
self.previous = previous
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.current = None
def push(self, val: int) -> None:
if self.current is None:
self.current = Node(val, val, None)
else:
self.current.next = Node(val, min(val, self.current.min), self.current)
self.current = self.current.next
def pop(self) -> None:
val = self.current.val
self.current = self.current.previous
return val
def top(self) -> int:
return self.current.val
def getMin(self) -> int:
return self.current.min