forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_895.java
31 lines (26 loc) · 875 Bytes
/
_895.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
public class _895 {
public static class Solution1 {
public static class FreqStack {
int counter;
PriorityQueue<int[]> maxHeap;
Map<Integer, Integer> map;
public FreqStack() {
maxHeap = new PriorityQueue<>((a, b) -> a[1] != b[1] ? b[1] - a[1] : b[2] - a[2]);
map = new HashMap<>();
}
public void push(int x) {
map.put(x, map.getOrDefault(x, 0) + 1);
maxHeap.offer(new int[]{x, map.get(x), counter++});
}
public int pop() {
int[] top = maxHeap.poll();
map.put(top[0], map.get(top[0]) - 1);
return top[0];
}
}
}
}