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