-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathMinStack.py
More file actions
28 lines (22 loc) · 801 Bytes
/
MinStack.py
File metadata and controls
28 lines (22 loc) · 801 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
# Time Complexity : O(1)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : Only when a new min appears, we store the previous min in the stack,
# so that when the current min is removed, we can restore the old one in constant time.
class MinStack:
def __init__(self):
self.stack = []
self.min = float('inf')
def push(self, val: int) -> None:
if val <= self.min:
self.stack.append(self.min)
self.min = val
self.stack.append(val)
def pop(self) -> None:
if self.min == self.stack.pop():
self.min = self.stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min