Skip to content

Commit 1472f2d

Browse files
committed
feat: add solutions to lc/lcci/lcof problems
Min Stack
1 parent 4afd410 commit 1472f2d

File tree

32 files changed

+1098
-829
lines changed

32 files changed

+1098
-829
lines changed

lcci/03.02.Min Stack/README.md

+91-47
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@
1111

1212
<!-- 这里可写通用的实现逻辑 -->
1313

14-
利用辅助栈存放栈的最小元素。
14+
**方法一:双栈**
15+
16+
我们用两个栈来实现,其中`stk1` 用来存储数据,`stk2` 用来存储当前栈中的最小值。初始时,`stk2` 中存储一个极大值。
17+
18+
- 当我们向栈中压入一个元素 `x` 时,我们将 `x` 压入 `stk1`,并将 `min(x, stk2[-1])` 压入 `stk2`
19+
- 当我们从栈中弹出一个元素时,我们将 `stk1``stk2` 的栈顶元素都弹出。
20+
- 当我们要获取当前栈中的栈顶元素时,我们只需要返回 `stk1` 的栈顶元素即可。
21+
- 当我们要获取当前栈中的最小值时,我们只需要返回 `stk2` 的栈顶元素即可。
22+
23+
时间复杂度:对于每个操作,时间复杂度均为 $O(1)$,空间复杂度 $O(n)$。
1524

1625
<!-- tabs:start -->
1726

@@ -57,39 +66,37 @@ class MinStack:
5766

5867
```java
5968
class MinStack {
60-
private Deque<Integer> s;
61-
private Deque<Integer> mins;
69+
private Deque<Integer> stk1 = new ArrayDeque<>();
70+
private Deque<Integer> stk2 = new ArrayDeque<>();
6271

6372
/** initialize your data structure here. */
6473
public MinStack() {
65-
s = new ArrayDeque<>();
66-
mins = new ArrayDeque<>();
67-
mins.push(Integer.MAX_VALUE);
74+
stk2.push(Integer.MAX_VALUE);
6875
}
6976

70-
public void push(int val) {
71-
s.push(val);
72-
mins.push(Math.min(mins.peek(), val));
77+
public void push(int x) {
78+
stk1.push(x);
79+
stk2.push(Math.min(x, stk2.peek()));
7380
}
7481

7582
public void pop() {
76-
s.pop();
77-
mins.pop();
83+
stk1.pop();
84+
stk2.pop();
7885
}
7986

8087
public int top() {
81-
return s.peek();
88+
return stk1.peek();
8289
}
8390

8491
public int getMin() {
85-
return mins.peek();
92+
return stk2.peek();
8693
}
8794
}
8895

8996
/**
9097
* Your MinStack object will be instantiated and called as such:
9198
* MinStack obj = new MinStack();
92-
* obj.push(val);
99+
* obj.push(x);
93100
* obj.pop();
94101
* int param_3 = obj.top();
95102
* int param_4 = obj.getMin();
@@ -100,36 +107,33 @@ class MinStack {
100107

101108
```cpp
102109
class MinStack {
103-
private:
104-
stack<int> stk;
105-
stack<int> minStk;
106-
107110
public:
108111
/** initialize your data structure here. */
109-
MinStack() = default;
112+
MinStack() {
113+
stk2.push(INT_MAX);
114+
}
110115

111116
void push(int x) {
112-
if (minStk.empty() || minStk.top() >= x) {
113-
minStk.push(x);
114-
}
115-
stk.push(x);
117+
stk1.push(x);
118+
stk2.push(min(x, stk2.top()));
116119
}
117120

118121
void pop() {
119-
int val = stk.top();
120-
stk.pop();
121-
if (val == minStk.top()) {
122-
minStk.pop();
123-
}
122+
stk1.pop();
123+
stk2.pop();
124124
}
125125

126126
int top() {
127-
return stk.top();
127+
return stk1.top();
128128
}
129129

130130
int getMin() {
131-
return minStk.top();
131+
return stk2.top();
132132
}
133+
134+
private:
135+
stack<int> stk1;
136+
stack<int> stk2;
133137
};
134138

135139
/**
@@ -188,39 +192,38 @@ class MinStack {
188192

189193
```go
190194
type MinStack struct {
191-
stack []int
192-
minStack []int
195+
stk1 []int
196+
stk2 []int
193197
}
194198

195199
/** initialize your data structure here. */
196200
func Constructor() MinStack {
197-
return MinStack{
198-
stack: make([]int, 0),
199-
minStack: make([]int, 0),
200-
}
201+
return MinStack{[]int{}, []int{math.MaxInt32}}
201202
}
202203

203204
func (this *MinStack) Push(x int) {
204-
this.stack = append(this.stack, x)
205-
if len(this.minStack) == 0 || x <= this.minStack[len(this.minStack)-1] {
206-
this.minStack = append(this.minStack, x)
207-
}
205+
this.stk1 = append(this.stk1, x)
206+
this.stk2 = append(this.stk2, min(x, this.stk2[len(this.stk2)-1]))
208207
}
209208

210209
func (this *MinStack) Pop() {
211-
v := this.stack[len(this.stack)-1]
212-
this.stack = this.stack[:len(this.stack)-1]
213-
if v == this.minStack[len(this.minStack)-1] {
214-
this.minStack = this.minStack[:len(this.minStack)-1]
215-
}
210+
this.stk1 = this.stk1[:len(this.stk1)-1]
211+
this.stk2 = this.stk2[:len(this.stk2)-1]
216212
}
217213

218214
func (this *MinStack) Top() int {
219-
return this.stack[len(this.stack)-1]
215+
return this.stk1[len(this.stk1)-1]
220216
}
221217

222218
func (this *MinStack) GetMin() int {
223-
return this.minStack[len(this.minStack)-1]
219+
return this.stk2[len(this.stk2)-1]
220+
}
221+
222+
func min(a, b int) int {
223+
if a < b {
224+
return a
225+
}
226+
return b
224227
}
225228

226229
/**
@@ -287,6 +290,47 @@ impl MinStack {
287290
*/
288291
```
289292

293+
### **C#**
294+
295+
```cs
296+
public class MinStack {
297+
private Stack<int> stk1 = new Stack<int>();
298+
private Stack<int> stk2 = new Stack<int>();
299+
300+
/** initialize your data structure here. */
301+
public MinStack() {
302+
stk2.Push(int.MaxValue);
303+
}
304+
305+
public void Push(int x) {
306+
stk1.Push(x);
307+
stk2.Push(Math.Min(x, GetMin()));
308+
}
309+
310+
public void Pop() {
311+
stk1.Pop();
312+
stk2.Pop();
313+
}
314+
315+
public int Top() {
316+
return stk1.Peek();
317+
}
318+
319+
public int GetMin() {
320+
return stk2.Peek();
321+
}
322+
}
323+
324+
/**
325+
* Your MinStack object will be instantiated and called as such:
326+
* MinStack obj = new MinStack();
327+
* obj.Push(x);
328+
* obj.Pop();
329+
* int param_3 = obj.Top();
330+
* int param_4 = obj.GetMin();
331+
*/
332+
```
333+
290334
### **...**
291335

292336
```

0 commit comments

Comments
 (0)