Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3d4ff04

Browse files
committed
feat: add solutions to lc problem: No.0155
No.0155.Min Stack
1 parent 39dbb89 commit 3d4ff04

File tree

10 files changed

+276
-200
lines changed

10 files changed

+276
-200
lines changed

‎solution/0100-0199/0155.Min Stack/README.md‎

Lines changed: 123 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ minStack.getMin(); --> 返回 -2.
6464
- 当我们要获取当前栈中的栈顶元素时,我们只需要返回 `stk1` 的栈顶元素即可。
6565
- 当我们要获取当前栈中的最小值时,我们只需要返回 `stk2` 的栈顶元素即可。
6666

67-
每个操作的时间复杂度为 $O(1)$。整体的空间复杂度为 $O(n)$。
67+
每个操作的时间复杂度为 $O(1)$。整体的空间复杂度为 $O(n)$,其中 $n$ 为栈中元素的个数
6868

6969
<!-- tabs:start -->
7070

@@ -79,9 +79,9 @@ class MinStack:
7979
self.stk1 = []
8080
self.stk2 = [inf]
8181

82-
def push(self, x: int) -> None:
83-
self.stk1.append(x)
84-
self.stk2.append(min(x, self.stk2[-1]))
82+
def push(self, val: int) -> None:
83+
self.stk1.append(val)
84+
self.stk2.append(min(val, self.stk2[-1]))
8585

8686
def pop(self) -> None:
8787
self.stk1.pop()
@@ -96,7 +96,7 @@ class MinStack:
9696

9797
# Your MinStack object will be instantiated and called as such:
9898
# obj = MinStack()
99-
# obj.push(x)
99+
# obj.push(val)
100100
# obj.pop()
101101
# param_3 = obj.top()
102102
# param_4 = obj.getMin()
@@ -111,14 +111,13 @@ class MinStack {
111111
private Deque<Integer> stk1 = new ArrayDeque<>();
112112
private Deque<Integer> stk2 = new ArrayDeque<>();
113113

114-
/** initialize your data structure here. */
115114
public MinStack() {
116115
stk2.push(Integer.MAX_VALUE);
117116
}
118117

119-
public void push(int x) {
120-
stk1.push(x);
121-
stk2.push(Math.min(x, stk2.peek()));
118+
public void push(int val) {
119+
stk1.push(val);
120+
stk2.push(Math.min(val, stk2.peek()));
122121
}
123122

124123
public void pop() {
@@ -138,7 +137,7 @@ class MinStack {
138137
/**
139138
* Your MinStack object will be instantiated and called as such:
140139
* MinStack obj = new MinStack();
141-
* obj.push(x);
140+
* obj.push(val);
142141
* obj.pop();
143142
* int param_3 = obj.top();
144143
* int param_4 = obj.getMin();
@@ -150,14 +149,13 @@ class MinStack {
150149
```cpp
151150
class MinStack {
152151
public:
153-
/** initialize your data structure here. */
154152
MinStack() {
155153
stk2.push(INT_MAX);
156154
}
157155

158-
void push(int x) {
159-
stk1.push(x);
160-
stk2.push(min(x, stk2.top()));
156+
void push(int val) {
157+
stk1.push(val);
158+
stk2.push(min(val, stk2.top()));
161159
}
162160

163161
void pop() {
@@ -181,55 +179,13 @@ private:
181179
/**
182180
* Your MinStack object will be instantiated and called as such:
183181
* MinStack* obj = new MinStack();
184-
* obj->push(x);
182+
* obj->push(val);
185183
* obj->pop();
186184
* int param_3 = obj->top();
187185
* int param_4 = obj->getMin();
188186
*/
189187
```
190188

191-
### **TypeScript**
192-
193-
```ts
194-
class MinStack {
195-
stack: number[];
196-
mins: number[];
197-
constructor() {
198-
this.stack = [];
199-
this.mins = [];
200-
}
201-
202-
push(x: number): void {
203-
this.stack.push(x);
204-
this.mins.push(Math.min(this.getMin(), x));
205-
}
206-
207-
pop(): void {
208-
this.stack.pop();
209-
this.mins.pop();
210-
}
211-
212-
top(): number {
213-
return this.stack[this.stack.length - 1];
214-
}
215-
216-
getMin(): number {
217-
return this.mins.length == 0
218-
? Infinity
219-
: this.mins[this.mins.length - 1];
220-
}
221-
}
222-
223-
/**
224-
* Your MinStack object will be instantiated and called as such:
225-
* var obj = new MinStack()
226-
* obj.push(x)
227-
* obj.pop()
228-
* var param_3 = obj.top()
229-
* var param_4 = obj.getMin()
230-
*/
231-
```
232-
233189
### **Go**
234190

235191
```go
@@ -238,14 +194,13 @@ type MinStack struct {
238194
stk2 []int
239195
}
240196

241-
/** initialize your data structure here. */
242197
func Constructor() MinStack {
243198
return MinStack{[]int{}, []int{math.MaxInt32}}
244199
}
245200

246-
func (this *MinStack) Push(x int) {
247-
this.stk1 = append(this.stk1, x)
248-
this.stk2 = append(this.stk2, min(x, this.stk2[len(this.stk2)-1]))
201+
func (this *MinStack) Push(val int) {
202+
this.stk1 = append(this.stk1, val)
203+
this.stk2 = append(this.stk2, min(val, this.stk2[len(this.stk2)-1]))
249204
}
250205

251206
func (this *MinStack) Pop() {
@@ -271,20 +226,110 @@ func min(a, b int) int {
271226
/**
272227
* Your MinStack object will be instantiated and called as such:
273228
* obj := Constructor();
274-
* obj.Push(x);
229+
* obj.Push(val);
275230
* obj.Pop();
276231
* param_3 := obj.Top();
277232
* param_4 := obj.GetMin();
278233
*/
279234
```
280235

236+
### **TypeScript**
237+
238+
```ts
239+
class MinStack {
240+
stk1: number[];
241+
stk2: number[];
242+
243+
constructor() {
244+
this.stk1 = [];
245+
this.stk2 = [Infinity];
246+
}
247+
248+
push(val: number): void {
249+
this.stk1.push(val);
250+
this.stk2.push(Math.min(val, this.stk2[this.stk2.length - 1]));
251+
}
252+
253+
pop(): void {
254+
this.stk1.pop();
255+
this.stk2.pop();
256+
}
257+
258+
top(): number {
259+
return this.stk1[this.stk1.length - 1];
260+
}
261+
262+
getMin(): number {
263+
return this.stk2[this.stk2.length - 1];
264+
}
265+
}
266+
267+
/**
268+
* Your MinStack object will be instantiated and called as such:
269+
* var obj = new MinStack()
270+
* obj.push(x)
271+
* obj.pop()
272+
* var param_3 = obj.top()
273+
* var param_4 = obj.getMin()
274+
*/
275+
```
276+
277+
### **JavaScript**
278+
279+
```js
280+
var MinStack = function () {
281+
this.stk1 = [];
282+
this.stk2 = [Infinity];
283+
};
284+
285+
/**
286+
* @param {number} val
287+
* @return {void}
288+
*/
289+
MinStack.prototype.push = function (val) {
290+
this.stk1.push(val);
291+
this.stk2.push(Math.min(this.stk2[this.stk2.length - 1], val));
292+
};
293+
294+
/**
295+
* @return {void}
296+
*/
297+
MinStack.prototype.pop = function () {
298+
this.stk1.pop();
299+
this.stk2.pop();
300+
};
301+
302+
/**
303+
* @return {number}
304+
*/
305+
MinStack.prototype.top = function () {
306+
return this.stk1[this.stk1.length - 1];
307+
};
308+
309+
/**
310+
* @return {number}
311+
*/
312+
MinStack.prototype.getMin = function () {
313+
return this.stk2[this.stk2.length - 1];
314+
};
315+
316+
/**
317+
* Your MinStack object will be instantiated and called as such:
318+
* var obj = new MinStack()
319+
* obj.push(val)
320+
* obj.pop()
321+
* var param_3 = obj.top()
322+
* var param_4 = obj.getMin()
323+
*/
324+
```
325+
281326
### **Rust**
282327

283328
```rust
284329
use std::collections::VecDeque;
285330
struct MinStack {
286-
stack: VecDeque<i32>,
287-
min_stack: VecDeque<i32>,
331+
stk1: VecDeque<i32>,
332+
stk2: VecDeque<i32>,
288333
}
289334

290335

@@ -294,31 +339,30 @@ struct MinStack {
294339
*/
295340
impl MinStack {
296341

297-
/** initialize your data structure here. */
298342
fn new() -> Self {
299-
Self { stack: VecDeque::new(), min_stack: VecDeque::new() }
343+
Self { stk1: VecDeque::new(), stk2: VecDeque::new() }
300344
}
301345

302346
fn push(&mut self, x: i32) {
303-
self.stack.push_back(x);
304-
if self.min_stack.is_empty() || *self.min_stack.back().unwrap() >= x {
305-
self.min_stack.push_back(x);
347+
self.stk1.push_back(x);
348+
if self.stk2.is_empty() || *self.stk2.back().unwrap() >= x {
349+
self.stk2.push_back(x);
306350
}
307351
}
308352

309353
fn pop(&mut self) {
310-
let val = self.stack.pop_back().unwrap();
311-
if *self.min_stack.back().unwrap() == val {
312-
self.min_stack.pop_back();
354+
let val = self.stk1.pop_back().unwrap();
355+
if *self.stk2.back().unwrap() == val {
356+
self.stk2.pop_back();
313357
}
314358
}
315359

316360
fn top(&self) -> i32 {
317-
*self.stack.back().unwrap()
361+
*self.stk1.back().unwrap()
318362
}
319363

320364
fn get_min(&self) -> i32 {
321-
*self.min_stack.back().unwrap()
365+
*self.stk2.back().unwrap()
322366
}
323367
}
324368

@@ -338,26 +382,25 @@ impl MinStack {
338382
public class MinStack {
339383
private Stack<int> stk1 = new Stack<int>();
340384
private Stack<int> stk2 = new Stack<int>();
341-
342-
/** initialize your data structure here. */
385+
343386
public MinStack() {
344387
stk2.Push(int.MaxValue);
345388
}
346-
389+
347390
public void Push(int x) {
348391
stk1.Push(x);
349392
stk2.Push(Math.Min(x, GetMin()));
350393
}
351-
394+
352395
public void Pop() {
353396
stk1.Pop();
354397
stk2.Pop();
355398
}
356-
399+
357400
public int Top() {
358401
return stk1.Peek();
359402
}
360-
403+
361404
public int GetMin() {
362405
return stk2.Peek();
363406
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /