Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. /**
  2. * initialize your data structure here.
  3. */
  4. var MinStack = function() {
  5. this.stack = [];
  6. this.minStack = [];
  7. };
  8.  
  9. /**
  10. * @param {number} x
  11. * @return {void}
  12. */
  13. MinStack.prototype.push = function(x) {
  14. this.stack.push(x);
  15. if(this.minStack.length === 0 || x <= this.minStack[this.minStack.length-1])
  16. {
  17. this.minStack.push(x);
  18. }
  19. };
  20.  
  21. /**
  22. * @return {void}
  23. */
  24. MinStack.prototype.pop = function() {
  25. var item = this.stack.pop();
  26. if(item == this.minStack[this.minStack.length-1])
  27. {
  28. this.minStack.pop();
  29. }
  30. };
  31.  
  32. /**
  33. * @return {number}
  34. */
  35. MinStack.prototype.top = function() {
  36. return this.stack[this.stack.length-1];
  37. };
  38.  
  39. /**
  40. * @return {number}
  41. */
  42. MinStack.prototype.getMin = function() {
  43. return this.minStack[this.minStack.length-1];
  44. };
  45.  
  46. /**
  47. * Your MinStack object will be instantiated and called as such:
  48. * var obj = Object.create(MinStack).createNew()
  49. * obj.push(x)
  50. * obj.pop()
  51. * var param_3 = obj.top()
  52. * var param_4 = obj.getMin()
  53. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement