Skip to content

Commit b5e91f1

Browse files
committed
feat: update solutions to lcof/lcci problems: Min Stack
1 parent 2e05bc1 commit b5e91f1

File tree

7 files changed

+128
-126
lines changed

7 files changed

+128
-126
lines changed

lcci/03.02.Min Stack/README.md

+27-27
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,27 @@ class MinStack:
2626
"""
2727
initialize your data structure here.
2828
"""
29-
self._s1 = []
30-
self._s2 = []
29+
self.s = []
30+
self.mins = [float('inf')]
3131

32-
33-
def push(self, x: int) -> None:
34-
self._s1.append(x)
35-
self._s2.append(x if len(self._s2) == 0 or self._s2[-1] >= x else self._s2[-1])
32+
def push(self, val: int) -> None:
33+
self.s.append(val)
34+
self.mins.append(min(self.mins[-1], val))
3635

3736
def pop(self) -> None:
38-
self._s1.pop()
39-
self._s2.pop()
37+
self.s.pop()
38+
self.mins.pop()
4039

4140
def top(self) -> int:
42-
return self._s1[-1]
41+
return self.s[-1]
4342

4443
def getMin(self) -> int:
45-
return self._s2[-1]
44+
return self.mins[-1]
4645

4746

4847
# Your MinStack object will be instantiated and called as such:
4948
# obj = MinStack()
50-
# obj.push(x)
49+
# obj.push(val)
5150
# obj.pop()
5251
# param_3 = obj.top()
5352
# param_4 = obj.getMin()
@@ -59,38 +58,39 @@ class MinStack:
5958

6059
```java
6160
class MinStack {
62-
private Stack<Integer> s1;
63-
private Stack<Integer> s2;
61+
private Deque<Integer> s;
62+
private Deque<Integer> mins;
6463

6564
/** initialize your data structure here. */
6665
public MinStack() {
67-
s1 = new Stack<>();
68-
s2 = new Stack<>();
66+
s = new ArrayDeque<>();
67+
mins = new ArrayDeque<>();
68+
mins.push(Integer.MAX_VALUE);
6969
}
70-
71-
public void push(int x) {
72-
s1.push(x);
73-
s2.push(s2.empty() || s2.peek() >= x ? x : s2.peek());
70+
71+
public void push(int val) {
72+
s.push(val);
73+
mins.push(Math.min(mins.peek(), val));
7474
}
75-
75+
7676
public void pop() {
77-
s1.pop();
78-
s2.pop();
77+
s.pop();
78+
mins.pop();
7979
}
80-
80+
8181
public int top() {
82-
return s1.peek();
82+
return s.peek();
8383
}
84-
84+
8585
public int getMin() {
86-
return s2.empty() ? -1 : s2.peek();
86+
return mins.peek();
8787
}
8888
}
8989

9090
/**
9191
* Your MinStack object will be instantiated and called as such:
9292
* MinStack obj = new MinStack();
93-
* obj.push(x);
93+
* obj.push(val);
9494
* obj.pop();
9595
* int param_3 = obj.top();
9696
* int param_4 = obj.getMin();

lcci/03.02.Min Stack/README_EN.md

+27-27
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,27 @@ class MinStack:
3939
"""
4040
initialize your data structure here.
4141
"""
42-
self._s1 = []
43-
self._s2 = []
42+
self.s = []
43+
self.mins = [float('inf')]
4444

45-
46-
def push(self, x: int) -> None:
47-
self._s1.append(x)
48-
self._s2.append(x if len(self._s2) == 0 or self._s2[-1] >= x else self._s2[-1])
45+
def push(self, val: int) -> None:
46+
self.s.append(val)
47+
self.mins.append(min(self.mins[-1], val))
4948

5049
def pop(self) -> None:
51-
self._s1.pop()
52-
self._s2.pop()
50+
self.s.pop()
51+
self.mins.pop()
5352

5453
def top(self) -> int:
55-
return self._s1[-1]
54+
return self.s[-1]
5655

5756
def getMin(self) -> int:
58-
return self._s2[-1]
57+
return self.mins[-1]
5958

6059

6160
# Your MinStack object will be instantiated and called as such:
6261
# obj = MinStack()
63-
# obj.push(x)
62+
# obj.push(val)
6463
# obj.pop()
6564
# param_3 = obj.top()
6665
# param_4 = obj.getMin()
@@ -70,38 +69,39 @@ class MinStack:
7069

7170
```java
7271
class MinStack {
73-
private Stack<Integer> s1;
74-
private Stack<Integer> s2;
72+
private Deque<Integer> s;
73+
private Deque<Integer> mins;
7574

7675
/** initialize your data structure here. */
7776
public MinStack() {
78-
s1 = new Stack<>();
79-
s2 = new Stack<>();
77+
s = new ArrayDeque<>();
78+
mins = new ArrayDeque<>();
79+
mins.push(Integer.MAX_VALUE);
8080
}
81-
82-
public void push(int x) {
83-
s1.push(x);
84-
s2.push(s2.empty() || s2.peek() >= x ? x : s2.peek());
81+
82+
public void push(int val) {
83+
s.push(val);
84+
mins.push(Math.min(mins.peek(), val));
8585
}
86-
86+
8787
public void pop() {
88-
s1.pop();
89-
s2.pop();
88+
s.pop();
89+
mins.pop();
9090
}
91-
91+
9292
public int top() {
93-
return s1.peek();
93+
return s.peek();
9494
}
95-
95+
9696
public int getMin() {
97-
return s2.empty() ? -1 : s2.peek();
97+
return mins.peek();
9898
}
9999
}
100100

101101
/**
102102
* Your MinStack object will be instantiated and called as such:
103103
* MinStack obj = new MinStack();
104-
* obj.push(x);
104+
* obj.push(val);
105105
* obj.pop();
106106
* int param_3 = obj.top();
107107
* int param_4 = obj.getMin();

lcci/03.02.Min Stack/Solution.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
class MinStack {
2-
private Stack<Integer> s1;
3-
private Stack<Integer> s2;
2+
private Deque<Integer> s;
3+
private Deque<Integer> mins;
44

55
/** initialize your data structure here. */
66
public MinStack() {
7-
s1 = new Stack<>();
8-
s2 = new Stack<>();
7+
s = new ArrayDeque<>();
8+
mins = new ArrayDeque<>();
9+
mins.push(Integer.MAX_VALUE);
910
}
1011

11-
public void push(int x) {
12-
s1.push(x);
13-
s2.push(s2.empty() || s2.peek() >= x ? x : s2.peek());
12+
public void push(int val) {
13+
s.push(val);
14+
mins.push(Math.min(mins.peek(), val));
1415
}
1516

1617
public void pop() {
17-
s1.pop();
18-
s2.pop();
18+
s.pop();
19+
mins.pop();
1920
}
2021

2122
public int top() {
22-
return s1.peek();
23+
return s.peek();
2324
}
2425

2526
public int getMin() {
26-
return s2.empty() ? -1 : s2.peek();
27+
return mins.peek();
2728
}
2829
}
2930

3031
/**
3132
* Your MinStack object will be instantiated and called as such:
3233
* MinStack obj = new MinStack();
33-
* obj.push(x);
34+
* obj.push(val);
3435
* obj.pop();
3536
* int param_3 = obj.top();
3637
* int param_4 = obj.getMin();

lcci/03.02.Min Stack/Solution.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,27 @@ def __init__(self):
44
"""
55
initialize your data structure here.
66
"""
7-
self._s1 = []
8-
self._s2 = []
7+
self.s = []
8+
self.mins = [float('inf')]
99

10-
def push(self, x: int) -> None:
11-
self._s1.append(x)
12-
self._s2.append(x if len(self._s2) ==
13-
0 or self._s2[-1] >= x else self._s2[-1])
10+
def push(self, val: int) -> None:
11+
self.s.append(val)
12+
self.mins.append(min(self.mins[-1], val))
1413

1514
def pop(self) -> None:
16-
self._s1.pop()
17-
self._s2.pop()
15+
self.s.pop()
16+
self.mins.pop()
1817

1918
def top(self) -> int:
20-
return self._s1[-1]
19+
return self.s[-1]
2120

2221
def getMin(self) -> int:
23-
return self._s2[-1]
22+
return self.mins[-1]
2423

2524

2625
# Your MinStack object will be instantiated and called as such:
2726
# obj = MinStack()
28-
# obj.push(x)
27+
# obj.push(val)
2928
# obj.pop()
3029
# param_3 = obj.top()
31-
# param_4 = obj.getMin()
30+
# param_4 = obj.getMin()

lcof/面试题30. 包含min函数的栈/README.md

+27-26
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,27 @@ class MinStack:
3434
"""
3535
initialize your data structure here.
3636
"""
37-
self.s1 = []
38-
self.s2 = []
37+
self.s = []
38+
self.mins = [float('inf')]
3939

40-
def push(self, x: int) -> None:
41-
self.s1.append(x)
42-
self.s2.append(x if not self.s2 or self.s2[-1] >= x else self.s2[-1])
40+
def push(self, val: int) -> None:
41+
self.s.append(val)
42+
self.mins.append(min(self.mins[-1], val))
4343

4444
def pop(self) -> None:
45-
self.s1.pop()
46-
self.s2.pop()
45+
self.s.pop()
46+
self.mins.pop()
4747

4848
def top(self) -> int:
49-
return self.s1[-1]
49+
return self.s[-1]
5050

5151
def min(self) -> int:
52-
return self.s2[-1]
52+
return self.mins[-1]
5353

5454

5555
# Your MinStack object will be instantiated and called as such:
5656
# obj = MinStack()
57-
# obj.push(x)
57+
# obj.push(val)
5858
# obj.pop()
5959
# param_3 = obj.top()
6060
# param_4 = obj.min()
@@ -64,38 +64,39 @@ class MinStack:
6464

6565
```java
6666
class MinStack {
67-
private Deque<Integer> s1;
68-
private Deque<Integer> s2;
67+
private Deque<Integer> s;
68+
private Deque<Integer> mins;
6969

7070
/** initialize your data structure here. */
7171
public MinStack() {
72-
s1 = new ArrayDeque<>();
73-
s2 = new ArrayDeque<>();
72+
s = new ArrayDeque<>();
73+
mins = new ArrayDeque<>();
74+
mins.push(Integer.MAX_VALUE);
7475
}
75-
76-
public void push(int x) {
77-
s1.push(x);
78-
s2.push(s2.isEmpty() || s2.peek() >= x ? x : s2.peek());
76+
77+
public void push(int val) {
78+
s.push(val);
79+
mins.push(Math.min(mins.peek(), val));
7980
}
80-
81+
8182
public void pop() {
82-
s1.pop();
83-
s2.pop();
83+
s.pop();
84+
mins.pop();
8485
}
85-
86+
8687
public int top() {
87-
return s1.peek();
88+
return s.peek();
8889
}
89-
90+
9091
public int min() {
91-
return s2.peek();
92+
return mins.peek();
9293
}
9394
}
9495

9596
/**
9697
* Your MinStack object will be instantiated and called as such:
9798
* MinStack obj = new MinStack();
98-
* obj.push(x);
99+
* obj.push(val);
99100
* obj.pop();
100101
* int param_3 = obj.top();
101102
* int param_4 = obj.min();

0 commit comments

Comments
 (0)