-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin-stack.js
More file actions
61 lines (60 loc) · 1.33 KB
/
min-stack.js
File metadata and controls
61 lines (60 loc) · 1.33 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 155. 最小栈
* 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
* https://leetcode-cn.com/problems/min-stack/
* push(x) -- 将元素 x 推入栈中
* pop() -- 删除栈顶的元素
* top() -- 获取栈顶元素
* getMin() -- 检索栈中的最小元素
*/
const MinStack = function () {
this.stack = []
this.mins = []
this.minIndex = -1
}
/**
* @param {number} x
* @returns {void}
*/
MinStack.prototype.push = function (x) {
this.stack.push(x)
if (this.minIndex === -1) {
this.minIndex = 0
this.mins.push(this.minIndex)
return
}
if (this.stack[this.minIndex] >= x) {
this.minIndex = this.stack.length - 1
this.mins.push(this.minIndex)
}
}
/**
* @param {number} x
* @returns {void}
*/
MinStack.prototype.pop = function () {
if (this.stack.length === 0) return
if (this.stack.length === this.minIndex + 1) {
this.mins.pop()
this.minIndex = this.mins[this.mins.length - 1]
}
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.stack[this.minIndex]
}
let obj = new MinStack()
obj.push(2147483646)
obj.push(2147483646)
obj.pop()
console.log(obj)
console.log(obj.getMin())