Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 5 ms, faster than 100.00% of Java online submissions for Min Stack.
- // Memory Usage: 38.6 MB, less than 44.93% of Java online submissions for Min Stack.
- class MinStack {
- Deque<Integer> stack;
- Deque<Integer> mins;
- /** initialize your data structure here. */
- public MinStack() {
- stack = new LinkedList<Integer>();
- mins = new LinkedList<Integer>();
- }
- public void push(int x) {
- stack.push(x);
- if (mins.isEmpty() || x <= mins.peek()) mins.push(x);
- }
- public void pop() {
- if (stack.peek().equals(mins.peek())) mins.pop();
- stack.pop();
- }
- public int top() {
- return stack.peek();
- }
- public int getMin() {
- return mins.peek();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment