Advertisement
K_S_

Untitled

Nov 3rd, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. // Runtime: 47 ms, faster than 72.11% of Java online submissions for Min Stack.
  2. // Memory Usage: 40.3 MB, less than 29.71% of Java online submissions for Min Stack.
  3. class MinStack {
  4.  
  5.     /** initialize your data structure here. */
  6.     private Deque<Integer> values;
  7.     private Deque<Integer> minStack;
  8.    
  9.     public MinStack() {
  10.         values = new ArrayDeque<>();
  11.         minStack = new ArrayDeque<>();
  12.     }
  13.  
  14.     public void push(int x) {
  15.         values.push(x);
  16.  
  17.         if (minStack.isEmpty()) {
  18.             minStack.push(x);
  19.         } else if (minStack.peek() > x) {
  20.             minStack.push(x);
  21.         } else {
  22.             minStack.push(minStack.peek());
  23.         }
  24.     }
  25.  
  26.     public void pop() {
  27.         int val = values.pop();
  28.         int min = minStack.pop();
  29.     }
  30.  
  31.     public int top() {
  32.         return values.peek();
  33.     }
  34.  
  35.     public int getMin() {
  36.         return minStack.peek();
  37.     }
  38. }
  39.  
  40. /**
  41.  * Your MinStack object will be instantiated and called as such:
  42.  * MinStack obj = new MinStack();
  43.  * obj.push(x);
  44.  * obj.pop();
  45.  * int param_3 = obj.top();
  46.  * int param_4 = obj.getMin();
  47.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement