-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminstack.py
More file actions
36 lines (22 loc) · 776 Bytes
/
minstack.py
File metadata and controls
36 lines (22 loc) · 776 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
# Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
# push(x) -- Push element x onto stack.
# pop() -- Removes the element on top of the stack.
# top() -- Get the top element.
# getMin() -- Retrieve the minimum element in the stack.
class MinStack(object):
def __init__(self):
self.nums = []
def push(self, x):
self.nums.append(x)
def pop(self):
return self.nums.pop()
def top(self):
return self.nums[len(self.nums)-1]
def getMin(self):
return min(self.nums)
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()