Advertisement
serega1112

155

Dec 23rd, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. class MinStack:
  2.  
  3.     def __init__(self):
  4.         """
  5.        initialize your data structure here.
  6.        """
  7.         self.main = []
  8.         self.mono = []
  9.  
  10.     def push(self, x: int) -> None:
  11.         self.main.append(x)
  12.         if not self.mono or x <= self.mono[-1]:
  13.             self.mono.append(x)
  14.        
  15.     def pop(self) -> None:
  16.         x = self.main.pop()
  17.         if self.mono and x == self.mono[-1]:
  18.             self.mono.pop()
  19.         return x
  20.  
  21.     def top(self) -> int:
  22.         return self.main[-1]        
  23.  
  24.     def getMin(self) -> int:
  25.         return self.mono[-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement