|
| 1 | +/** |
| 2 | + * @description Initialize stack as an array |
| 3 | + */ |
| 4 | +const MinStack = function () { |
| 5 | + this.stack = []; |
| 6 | +}; |
| 7 | + |
| 8 | +/** |
| 9 | +* @description Pushes element to the top of the stack |
| 10 | +* @param {Number} value |
| 11 | +* @return {Void} |
| 12 | +*/ |
| 13 | +MinStack.prototype.push = function (value) { |
| 14 | + const elem = { value, min: this.stack.length === 0 ? value : Math.min(value, this.getMin()) }; |
| 15 | + this.stack.push(elem); |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | +* @description Returns element on the top of the stack and removes it |
| 20 | +* @return {Void} |
| 21 | +*/ |
| 22 | +MinStack.prototype.pop = function () { |
| 23 | + const { value } = this.stack.pop(); |
| 24 | + return value; |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | +* @description Returns element on the top of the stack |
| 29 | +* @return {Number} |
| 30 | +*/ |
| 31 | +MinStack.prototype.top = function () { |
| 32 | + const { value } = this.stack[this.stack.length - 1]; |
| 33 | + return value; |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | +* @description Returns the min element in the stack |
| 38 | +* @return {Number} |
| 39 | +*/ |
| 40 | +MinStack.prototype.getMin = function () { |
| 41 | + const { min } = this.stack[this.stack.length - 1]; |
| 42 | + return min; |
| 43 | +}; |
0 commit comments