-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
-
思路
- stack + map
-
刷15mins
-
刷5mins
Map<Integer, Integer> freq;
Map<Integer, Stack<Integer>> group;
int maxFreq;
public FreqStack895() {
freq = new HashMap<>();
group = new HashMap<>();
maxFreq = 0;
}
public void push(int val) {
int f = freq.getOrDefault(val, 0) + 1;
freq.put(val, f);
if (f > maxFreq)
maxFreq = f;
if (!group.containsKey(f)) {
group.put(f, new Stack<>());
}
group.get(f).push(val);
}
public int pop() {
int x = group.get(maxFreq).pop();
freq.put(x, freq.get(x) - 1);
if (group.get(maxFreq).size() == 0)
maxFreq--;
return x;
}