Guest User

Untitled

a guest
Mar 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. class MinStack:
  2. def __init__(self):
  3. """
  4. initialize your data structure here.
  5. """
  6. self.stack = []
  7. self.min_stack = []
  8.  
  9. def push(self, x):
  10. """
  11. :type x: int
  12. :rtype: void
  13. """
  14. self.stack.append(x)
  15. if self.min_stack != []:
  16. if x < self.min_stack[-1]:
  17. self.min_stack.append(x)
  18. else:
  19. self.min_stack.append(self.min_stack[-1])
  20. else:
  21. self.min_stack.append(x)
  22.  
  23. def pop(self):
  24. """
  25. :rtype: void
  26. """
  27. self.stack.pop()
  28. self.min_stack.pop()
  29.  
  30. def top(self):
  31. """
  32. :rtype: int
  33. """
  34. return self.stack[-1]
  35.  
  36. def getMin(self):
  37. """
  38. :rtype: int
  39. """
  40. return self.min_stack[-1]
  41.  
  42.  
  43. # Your MinStack object will be instantiated and called as such:
  44. # obj = MinStack()
  45. # obj.push(x)
  46. # obj.pop()
  47. # param_3 = obj.top()
  48. # param_4 = obj.getMin()
Add Comment
Please, Sign In to add comment