Skip to content

Commit ba32672

Browse files
author
Hieu Luong
committed
Solve Problem 155 - Min Stack
1 parent 370d175 commit ba32672

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/stack/Problem155_MinStack.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package stack;
2+
3+
import java.util.Stack;
4+
5+
public class Problem155_MinStack {
6+
7+
class MinStack {
8+
9+
Stack<int[]> stack = new Stack<>();
10+
11+
/** initialize your data structure here. */
12+
public MinStack() {
13+
}
14+
15+
public void push(int x) {
16+
int min = (stack.isEmpty()) ? x : Math.min(x, getMin());
17+
stack.push(new int[] {x, min});
18+
}
19+
20+
public void pop() {
21+
stack.pop();
22+
}
23+
24+
public int top() {
25+
return stack.peek()[0];
26+
}
27+
28+
public int getMin() {
29+
return stack.peek()[1];
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)