Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 47 ms, faster than 72.11% of Java online submissions for Min Stack.
- // Memory Usage: 39.7 MB, less than 39.13% of Java online submissions for Min Stack.
- class MinStack {
- PriorityQueue<Integer> pq;
- Deque<Integer> stack;
- /** initialize your data structure here. */
- public MinStack() {
- stack = new LinkedList<Integer>();
- pq = new PriorityQueue<Integer>();
- }
- public void push(int x) {
- stack.addFirst(x);
- pq.add(x);
- }
- public void pop() {
- int top = stack.removeFirst();
- pq.remove(top);
- }
- public int top() {
- return stack.getFirst();
- }
- public int getMin() {
- return pq.peek();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement