forked from Turingfly/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinStack.java
48 lines (42 loc) · 936 Bytes
/
MinStack.java
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
package chapter03StacksAndQueues;
import java.util.Stack;
/**
*
* Problem: How would you design a stack which, in addition to push and pop, has
* a function min which returns the minimum element? Push, pop and min should
* all operate in 0(1) time.
*
* Solution: Use an additional stack which keeps track of the mins.
*
*/
public class MinStack extends Stack<Integer> {
private Stack<Integer> stack2 = new Stack<>();
public void push(int val) {
// must be <=
if (val <= min()) {
stack2.push(val);
}
super.push(val);
}
public Integer pop() {
int val = super.pop();
if (val == min()) {
stack2.pop();
}
return val;
}
public Integer min() {
if (stack2.isEmpty()) {
return Integer.MAX_VALUE;
} else {
return stack2.peek();
}
}
public static void main(String[] args) {
MinStack min = new MinStack();
min.push(3);
min.push(4);
min.push(2);
System.out.println(min.min());
}
}