-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin-stack.js
More file actions
40 lines (36 loc) · 761 Bytes
/
min-stack.js
File metadata and controls
40 lines (36 loc) · 761 Bytes
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
var MinStack = function () {
this.stack = [];
this.minimumStack = [];
};
/**
* @param {number} val
* @return {void}
*/
MinStack.prototype.push = function (val) {
this.stack.push(val);
const currentMinimum = this.getMin();
if (val < currentMinimum || currentMinimum === undefined) {
this.minimumStack.push(val);
} else {
this.minimumStack.push(currentMinimum);
}
};
/**
* @return {void}
*/
MinStack.prototype.pop = function () {
this.minimumStack.pop();
return this.stack.pop();
};
/**
* @return {number}
*/
MinStack.prototype.top = function () {
return this.stack[this.stack.length - 1];
};
/**
* @return {number}
*/
MinStack.prototype.getMin = function () {
return this.minimumStack[this.minimumStack.length - 1];
};