Advertisement
ogv

Untitled

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