Skip to content

Commit 7d9dce8

Browse files
authored
feat(#155): 增加辅助栈的解法
1 parent 59e320c commit 7d9dce8

File tree

1 file changed

+152
-30
lines changed

1 file changed

+152
-30
lines changed

problems/155.min-stack.md

Lines changed: 152 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
## 题目地址
1+
# 题目地址
22

33
https://leetcode.com/problems/min-stack/description/
44

5-
## 题目描述
5+
# 题目描述
66

77
```
88
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
@@ -23,12 +23,14 @@ minStack.getMin(); --> Returns -2.
2323
2424
```
2525

26+
# 差值法
27+
2628
## 思路
2729

2830
符合直觉的方法是,每次对栈进行修改操作(push和pop)的时候更新最小值。 然后getMin只需要返回我们计算的最小值即可,
2931
top也是直接返回栈顶元素即可。 这种做法每次修改栈都需要更新最小值,因此时间复杂度是O(n).
3032

31-
![](https://tva1.sinaimg.cn/large/0082zybply1gburknpsi5j30d609g3z0.jpg)
33+
![](https://pic.leetcode-cn.com/7beed41b8dc0325445721a36b7db34e1af902441b67996d2eeadcb1f5a5e33d9.jpg)
3234

3335
是否有更高效的算法呢?答案是有的。
3436

@@ -50,8 +52,8 @@ pop或者top的时候:
5052

5153
- 如果栈顶元素大于0,说明它对最小值`没有影响`,上一个最小值就是上上个最小值。
5254

53-
![](https://tva1.sinaimg.cn/large/0082zybply1gburkz4jwjj30ji0k1jtm.jpg)
54-
![](https://tva1.sinaimg.cn/large/0082zybply1gburlamkrcj30ht0b4jsh.jpg)
55+
![](https://pic.leetcode-cn.com/7da0473d92d70bb47ce7b62303c062e5f517b09d1bf501c4ad341b65415d5c43.jpg)
56+
![](https://pic.leetcode-cn.com/aefec54238c942c484837ea6c724304fb179d3d64f110481d955d9eea65c4fc5.jpg)
5557

5658
## 关键点
5759

@@ -75,7 +77,7 @@ Javascript Code:
7577
*/
7678
var MinStack = function() {
7779
this.stack = [];
78-
this.min = Number.MAX_VALUE;
80+
this.minV = Number.MAX_VALUE;
7981
};
8082

8183
/**
@@ -84,45 +86,45 @@ var MinStack = function() {
8486
*/
8587
MinStack.prototype.push = function(x) {
8688
// update 'min'
87-
const min = this.min;
88-
if (x < this.min) {
89-
this.min = x;
89+
const minV = this.minV;
90+
if (x < this.minV) {
91+
this.minV = x;
9092
}
91-
return this.stack.push(x - min);
93+
return this.stack.push(x - minV);
9294
};
9395

9496
/**
9597
* @return {void}
9698
*/
9799
MinStack.prototype.pop = function() {
98100
const item = this.stack.pop();
99-
const min = this.min;
101+
const minV = this.minV;
100102

101103
if (item < 0) {
102-
this.min = min - item;
103-
return min;
104+
this.minV = minV - item;
105+
return minV;
104106
}
105-
return item + min;
107+
return item + minV;
106108
};
107109

108110
/**
109111
* @return {number}
110112
*/
111113
MinStack.prototype.top = function() {
112114
const item = this.stack[this.stack.length - 1];
113-
const min = this.min;
115+
const minV = this.minV;
114116

115117
if (item < 0) {
116-
return min;
118+
return minV;
117119
}
118-
return item + min;
120+
return item + minV;
119121
};
120122

121123
/**
122124
* @return {number}
123125
*/
124-
MinStack.prototype.getMin = function() {
125-
return this.min;
126+
MinStack.prototype.min = function() {
127+
return this.minV;
126128
};
127129

128130
/**
@@ -131,7 +133,7 @@ MinStack.prototype.getMin = function() {
131133
* obj.push(x)
132134
* obj.pop()
133135
* var param_3 = obj.top()
134-
* var param_4 = obj.getMin()
136+
* var param_4 = obj.min()
135137
*/
136138
```
137139

@@ -144,32 +146,32 @@ class MinStack:
144146
"""
145147
initialize your data structure here.
146148
"""
147-
self.min = float('inf')
149+
self.minV = float('inf')
148150
self.stack = []
149151

150152
def push(self, x: int) -> None:
151-
self.stack.append(x - self.min)
152-
if x < self.min:
153-
self.min = x
153+
self.stack.append(x - self.minV)
154+
if x < self.minV:
155+
self.minV = x
154156

155157
def pop(self) -> None:
156158
if not self.stack:
157159
return
158160
tmp = self.stack.pop()
159161
if tmp < 0:
160-
self.min -= tmp
162+
self.minV -= tmp
161163

162164
def top(self) -> int:
163165
if not self.stack:
164166
return
165167
tmp = self.stack[-1]
166168
if tmp < 0:
167-
return self.min
169+
return self.minV
168170
else:
169-
return self.min + tmp
171+
return self.minV + tmp
170172

171-
def getMin(self) -> int:
172-
return self.min
173+
def min(self) -> int:
174+
return self.minV
173175

174176

175177

@@ -178,5 +180,125 @@ class MinStack:
178180
# obj.push(x)
179181
# obj.pop()
180182
# param_3 = obj.top()
181-
# param_4 = obj.getMin()
183+
# param_4 = obj.min()
182184
```
185+
186+
**复杂度分析**
187+
- 时间复杂度:O(1)
188+
- 空间复杂度:O(1)
189+
190+
191+
# 两个栈
192+
193+
## 思路
194+
195+
我们使用两个栈:
196+
197+
- 一个栈存放全部的元素,push,pop都是正常操作这个正常栈。
198+
- 另一个存放最小栈。 每次push,如果比最小栈的栈顶还小,我们就push进最小栈,否则不操作
199+
- 每次pop的时候,我们都判断其是否和最小栈栈顶元素相同,如果相同,那么我们pop掉最小栈的栈顶元素即可
200+
201+
## 关键点
202+
203+
- 往minstack中 push的判断条件。 应该是stack为空或者x小于等于minstack栈顶元素
204+
205+
206+
## 代码
207+
208+
JavaScript:
209+
210+
```js
211+
/**
212+
* initialize your data structure here.
213+
*/
214+
var MinStack = function() {
215+
this.stack = []
216+
this.minStack = []
217+
};
218+
219+
/**
220+
* @param {number} x
221+
* @return {void}
222+
*/
223+
MinStack.prototype.push = function(x) {
224+
this.stack.push(x)
225+
if (this.minStack.length == 0 || x <= this.minStack[this.minStack.length - 1]) {
226+
this.minStack.push(x)
227+
}
228+
};
229+
230+
/**
231+
* @return {void}
232+
*/
233+
MinStack.prototype.pop = function() {
234+
const x = this.stack.pop()
235+
if (x !== void 0 && x === this.minStack[this.minStack.length - 1]) {
236+
this.minStack.pop()
237+
}
238+
};
239+
240+
/**
241+
* @return {number}
242+
*/
243+
MinStack.prototype.top = function() {
244+
return this.stack[this.stack.length - 1]
245+
};
246+
247+
/**
248+
* @return {number}
249+
*/
250+
MinStack.prototype.min = function() {
251+
return this.minStack[this.minStack.length - 1]
252+
};
253+
254+
/**
255+
* Your MinStack object will be instantiated and called as such:
256+
* var obj = new MinStack()
257+
* obj.push(x)
258+
* obj.pop()
259+
* var param_3 = obj.top()
260+
* var param_4 = obj.min()
261+
*/
262+
```
263+
264+
265+
Python3:
266+
267+
```python
268+
class MinStack:
269+
270+
def __init__(self):
271+
"""
272+
initialize your data structure here.
273+
"""
274+
self.stack = []
275+
self.minstack = []
276+
277+
def push(self, x: int) -> None:
278+
self.stack.append(x)
279+
if not self.minstack or x <= self.minstack[-1]:
280+
self.minstack.append(x)
281+
282+
def pop(self) -> None:
283+
tmp = self.stack.pop()
284+
if tmp == self.minstack[-1]:
285+
self.minstack.pop()
286+
287+
def top(self) -> int:
288+
return self.stack[-1]
289+
290+
def min(self) -> int:
291+
return self.minstack[-1]
292+
293+
294+
# Your MinStack object will be instantiated and called as such:
295+
# obj = MinStack()
296+
# obj.push(x)
297+
# obj.pop()
298+
# param_3 = obj.top()
299+
# param_4 = obj.min()
300+
```
301+
302+
**复杂度分析**
303+
- 时间复杂度:O(1)
304+
- 空间复杂度:O(N)

0 commit comments

Comments
 (0)