155. Min Stack
Solution
class MinStack {
/** initialize your data structure here. */
Stack<Integer> s;
int min = Integer.MAX_VALUE;
public MinStack() {
s = new Stack<>();
}
public void push(int x) {
if(x <= min){
s.push(min);
min = x;
}
s.push(x);
}
public void pop() {
if(min == s.peek()){
s.pop();
min = s.peek();
}
s.pop();
}
public int top() {
return s.peek();
}
public int getMin() {
return min;
}
}
Last updated
Was this helpful?