-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinStack.js
More file actions
141 lines (128 loc) · 3.57 KB
/
MinStack.js
File metadata and controls
141 lines (128 loc) · 3.57 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
最小栈
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
push(x) —— 将元素 x 推入栈中。
pop() —— 删除栈顶的元素。
top() —— 获取栈顶元素。
getMin() —— 检索栈中的最小元素。
*/
/*
示例:
输入:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
输出:
[null,null,null,null,-3,null,0,-2]
解释:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.getMin(); --> 返回 -2.
提示:
pop、top 和 getMin 操作总是在 非空栈 上调用
*/
/**
* initialize your data structure here.
*/
const MinStack = function() {
this.arr = [];
this.helper = []; //单调递减的辅助栈,栈顶元素永远是arr中最小的元素
};
/**
* @param {number} val
* @return {void}
*/
MinStack.prototype.push = function(val) {
this.arr.push(val);
if (this.helper.length === 0) {
this.helper.push(val);
} else {
const helperTop = this.helper[this.helper.length - 1];
// 如果辅助栈的栈顶元素大于入栈元素,则把栈顶元素入栈;否则把辅助栈的栈顶元素再一次入栈
if (helperTop > val) {
this.helper.push(val);
} else {
this.helper.push(helperTop);
}
}
};
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
this.arr.pop();
this.helper.pop();
};
/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.arr[this.arr.length - 1];
};
/**
* @return {number}
*/
MinStack.prototype.getMin = function() {
return this.helper[this.helper.length - 1];
};
const obj = new MinStack();
obj.push(2); // arr:2 helper:2
obj.push(1); // arr:2,1 helper:2,1
obj.push(3); // arr:2,1,3 helper:2,1,1
obj.pop(); // arr:2,1 helper:2,1
const param_1 = obj.top();
const param_2 = obj.getMin();
console.log(param_1, param_2); // 1 1
// 该算法时间复杂度为O(1),空间复杂度为O(n)
//解法2:用一个栈保存元素和最小值的差值,用一个min来保存当前的最小值
function MinStack2() {
this.min = 0;
this.stack = [];
}
MinStack2.prototype.push = function(val) {
if(this.stack.length === 0) {
this.min = val;
this.stack.push(0);
} else {
const num = val - this.min;
if(num < 0) {
this.min = val; // 当前值比最小值小,更新最小值
}
this.stack.push(num); //差值入栈
}
};
MinStack2.prototype.pop = function() {
const num = this.stack.pop();
if(this.min <0) {
// 当最小值为负数时,说明入栈的元素更小,要更新最小值为最小值-当前值
this.min = this.min - num;
}
};
MinStack2.prototype.top = function() {
const num = this.stack[this.stack.length - 1];
// 当num为负数时,说明刚刚更新过最小值,num就是top值,直接返回num;否则,top为num和最小值的和
if( num < 0) {
return num;
}
return num + this.min;
};
MinStack2.prototype.getMin = function(){
return this.min;
};
//该算法时间复杂度和空间复杂度均为O(1)
const obj2 = new MinStack2();
obj2.push(2); // stack:0 min:2
obj2.push(1); // stack:0 -1 min:1
obj2.push(3); // stack 0 -1 2 min:1
obj2.push(0); // stack 0 -1 2 -1 min:0
obj2.push(-2); // stack 0 -1 2 -1 -2 min:-2
obj2.push(-1); // stack 0 -1 2 -1 1 min:-2
obj2.push(-3); // stack 0 -1 2 -1 -1 min:-3
obj2.pop(); //
const param_3 = obj2.top();
const param_4 = obj2.getMin();
console.log(param_3, param_4);