-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathMinimumStackTest.java
61 lines (48 loc) · 1.34 KB
/
MinimumStackTest.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
49
50
51
52
53
54
55
56
57
58
59
60
61
package datastructure.stack;
import org.junit.Test;
import java.util.Stack;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class MinimumStackTest {
/*
TASK
Stack에 저장된 값들 중 최소값을 반환하는 minStack() 함수를 구현한다.
*/
@Test
public void test() {
MyStack stack = new MyStack();
stack.push(3);
stack.push(5);
stack.push(4);
stack.push(2);
stack.push(6);
assertThat(stack.min(), is(2));
}
public class MyStack {
private Stack<Integer> stack;
private Stack<Integer> minStack;
public MyStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int newVal) {
if (minStack.isEmpty() || newVal <= minStack.peek()) {
minStack.push(newVal);
}
stack.push(newVal);
}
public int pop() {
int val = stack.pop();
if (!minStack.isEmpty() && val == minStack.peek()) {
minStack.pop();
}
return val;
}
public int min() {
if (minStack.isEmpty()) {
throw new RuntimeException("");
}
return minStack.peek();
}
}
}