Advertisement
smj007

Untitled

Feb 11th, 2024
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. class MinStack:
  2.  
  3.     def __init__(self):
  4.         self.stack = []
  5.  
  6.     def push(self, val: int) -> None:
  7.         if len(self.stack) > 0:
  8.             self.stack.append([val, min(self.getMin(), val)])
  9.         else:
  10.             self.stack.append([val, val])
  11.        
  12.     def pop(self) -> None:
  13.         self.stack.pop()
  14.        
  15.     def top(self) -> int:
  16.         return self.stack[-1][0]
  17.  
  18.     def getMin(self) -> int:
  19.         return self.stack[-1][1]
  20.        
  21.  
  22.  
  23. # Your MinStack object will be instantiated and called as such:
  24. # obj = MinStack()
  25. # obj.push(val)
  26. # obj.pop()
  27. # param_3 = obj.top()
  28. # param_4 = obj.getMin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement