ogv

Untitled

ogv
Nov 3rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. // Runtime: 5 ms, faster than 100.00% of Java online submissions for Min Stack.
  2. // Memory Usage: 38.6 MB, less than 44.93% of Java online submissions for Min Stack.
  3. class MinStack {
  4.     Deque<Integer> stack;
  5.     Deque<Integer> mins;
  6.    
  7.     /** initialize your data structure here. */
  8.     public MinStack() {
  9.         stack = new LinkedList<Integer>();
  10.         mins = new LinkedList<Integer>();
  11.     }
  12.    
  13.     public void push(int x) {
  14.         stack.push(x);
  15.         if (mins.isEmpty() || x <= mins.peek()) mins.push(x);
  16.     }
  17.    
  18.     public void pop() {
  19.         if (stack.peek().equals(mins.peek())) mins.pop();
  20.         stack.pop();
  21.     }
  22.    
  23.     public int top() {
  24.         return stack.peek();
  25.     }
  26.    
  27.     public int getMin() {
  28.         return mins.peek();
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment