https://leetcode.com/problems/min-stack/
- Stack
- Design
Store the current minimum alongside each pushed value so getMin stays O(1).
O(1) per operation
O(n)
import java.util.*;
class MinStack {
private Stack<int[]> stack = new Stack<>();
public void push(int val) {
int min = stack.isEmpty() ? val : Math.min(val, stack.peek()[1]);
stack.push(new int[]{val, min});
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek()[0];
}
public int getMin() {
return stack.peek()[1];
}
}