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;
}
https://leetcode-cn.com/problems/maximum-frequency-stack/
思路
[ ] 刷15mins
[ ] 刷5mins