Skip to content

Commit 44c5ed2

Browse files
committed
Time: 112 ms, Memory: 49.9 MB - LeetHub
1 parent 7947bd3 commit 44c5ed2

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

0min-stack/0min-stack.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
var MinStack = function() {
3+
this._top = -1;
4+
this.data = [];
5+
this.min = Number.MAX_SAFE_INTEGER;
6+
};
7+
8+
/**
9+
* @param {number} val
10+
* @return {void}
11+
*/
12+
MinStack.prototype.push = function(val) {
13+
this.data[++this._top] = this.min;
14+
this.data[++this._top] = val;
15+
val < this.min && (this.min = val);
16+
};
17+
18+
/**
19+
* @return {void}
20+
*/
21+
MinStack.prototype.pop = function() {
22+
this.min = this.data[--this._top];
23+
--this._top;
24+
};
25+
26+
/**
27+
* @return {number}
28+
*/
29+
MinStack.prototype.top = function() {
30+
return this.data[this._top];
31+
};
32+
33+
/**
34+
* @return {number}
35+
*/
36+
MinStack.prototype.getMin = function() {
37+
return this.min;
38+
};
39+
40+
/**
41+
* Your MinStack object will be instantiated and called as such:
42+
* var obj = new MinStack()
43+
* obj.push(val)
44+
* obj.pop()
45+
* var param_3 = obj.top()
46+
* var param_4 = obj.getMin()
47+
*/

0 commit comments

Comments
 (0)