Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
public class MinStack {
/** initialize your dat_st structure here. */
Stack<Integer> dat_st = new Stack<>();
Stack<Integer> min_st = new Stack<>();
public MinStack() {
dat_st.push(-1);
min_st.push(Integer.MAX_VALUE);
}
public void push(int x) {
dat_st.push(x);
if(x <= min_st.peek())
min_st.push(x);
}
public void pop() {
int x = dat_st.pop();
if(x == min_st.peek())
min_st.pop();
}
public int top() {
return dat_st.peek();
}
public int getMin() {
return min_st.peek();
}
}