-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe_min_stack.py
More file actions
29 lines (25 loc) · 824 Bytes
/
Copy pathe_min_stack.py
File metadata and controls
29 lines (25 loc) · 824 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
################################################
# LeetCode Problem Number : 155
# Difficulty Level : Easy
# URL : https://leetcode.com/problems/min-stack/
################################################
class MinStack:
def __init__(self):
self.arr = []
def push(self, x: int) -> None:
"""store every element as a tuple :
(element, minimum stack element till that index position)
"""
self.arr.append((x, min(self.getMin(), x)))
def pop(self) -> None:
if len(self.arr):
self.arr.pop()
def top(self) -> int:
if len(self.arr):
return self.arr[-1][0]
def getMin(self) -> int:
""" constant time operation """
if len(self.arr):
return self.arr[-1][1]
else:
return float("inf")