Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * initialize your data structure here.
- */
- var MinStack = function() {
- this.repo = [];
- };
- /**
- * @param {number} x
- * @return {void}
- */
- MinStack.prototype.push = function(x) {
- if (!isNaN(x)) {
- this.repo.push(x);
- }
- };
- /**
- * @return {void}
- */
- MinStack.prototype.pop = function() {
- return this.repo.pop();
- };
- /**
- * @return {number}
- */
- MinStack.prototype.top = function() {
- return this.repo[this.repo.length - 1];
- };
- /**
- * @return {number}
- */
- MinStack.prototype.getMin = function() {
- if (this.repo) {
- const copy = this.repo.slice(0);
- return copy.sort((a,b) => a - b)[0];
- }
- };
- /**
- * initialize your data structure here.
- */
- var MinStack = function() {
- this.repo = [];
- this.minRepo = [];
- };
- /**
- * @param {number} x
- * @return {void}
- */
- MinStack.prototype.push = function(x) {
- if (!isNaN(x)) {
- if (!this.minRepo.length || x <= this.minRepo[0]) {
- this.minRepo.unshift(x);
- }
- this.repo.push(x);
- }
- };
- /**
- * @return {void}
- */
- MinStack.prototype.pop = function() {
- if (this.repo.pop() === this.minRepo[0]) {
- this.minRepo.shift();
- }
- };
- /**
- * @return {number}
- */
- MinStack.prototype.top = function() {
- return this.repo[this.repo.length - 1];
- };
- /**
- * @return {number}
- */
- MinStack.prototype.getMin = function() {
- if (this.minRepo.length) {
- return this.minRepo[0];
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement