-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathleetcode_155_MinStack.py
More file actions
42 lines (34 loc) · 1.1 KB
/
Copy pathleetcode_155_MinStack.py
File metadata and controls
42 lines (34 loc) · 1.1 KB
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
# 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.
class MinStack:
def __init__(self):
self.stack = []
self.minstack = []
def push(self, val: int) -> None:
self.stack.append(val)
minstackval = val
if self.minstack:
minstackval = self.minstack[-1]
val = min(val, minstackval)
self.minstack.append(val)
def pop(self) -> None:
self.stack.pop()
self.minstack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.minstack[1]
# Your MinStack object will be instantiated and called as such:
# for each position have minimum
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()