Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. /**
  2. * initialize your data structure here.
  3. */
  4. var MinStack = function() {
  5. this.repo = [];
  6. };
  7.  
  8. /**
  9. * @param {number} x
  10. * @return {void}
  11. */
  12. MinStack.prototype.push = function(x) {
  13. if (!isNaN(x)) {
  14. this.repo.push(x);
  15. }
  16. };
  17.  
  18. /**
  19. * @return {void}
  20. */
  21. MinStack.prototype.pop = function() {
  22. return this.repo.pop();
  23. };
  24.  
  25. /**
  26. * @return {number}
  27. */
  28. MinStack.prototype.top = function() {
  29. return this.repo[this.repo.length - 1];
  30. };
  31.  
  32. /**
  33. * @return {number}
  34. */
  35. MinStack.prototype.getMin = function() {
  36. if (this.repo) {
  37. const copy = this.repo.slice(0);
  38. return copy.sort((a,b) => a - b)[0];
  39. }
  40. };
  41.  
  42. /**
  43. * initialize your data structure here.
  44. */
  45. var MinStack = function() {
  46. this.repo = [];
  47. this.minRepo = [];
  48. };
  49.  
  50. /**
  51. * @param {number} x
  52. * @return {void}
  53. */
  54. MinStack.prototype.push = function(x) {
  55. if (!isNaN(x)) {
  56. if (!this.minRepo.length || x <= this.minRepo[0]) {
  57. this.minRepo.unshift(x);
  58. }
  59. this.repo.push(x);
  60. }
  61. };
  62.  
  63. /**
  64. * @return {void}
  65. */
  66. MinStack.prototype.pop = function() {
  67. if (this.repo.pop() === this.minRepo[0]) {
  68. this.minRepo.shift();
  69. }
  70. };
  71.  
  72. /**
  73. * @return {number}
  74. */
  75. MinStack.prototype.top = function() {
  76. return this.repo[this.repo.length - 1];
  77. };
  78.  
  79. /**
  80. * @return {number}
  81. */
  82. MinStack.prototype.getMin = function() {
  83. if (this.minRepo.length) {
  84. return this.minRepo[0];
  85. }
  86. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement