Leetcode 931 Solution
This article provides solution to leetcode question 931 (maximum-frequency-stack)
Access this page by simply typing in "lcs 931" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/maximum-frequency-stack
Solution
class FreqStack {
int maxFreq;
unordered_map<int, vector<int>> values;
unordered_map<int, int> freq;
public:
FreqStack() {
maxFreq = 0;
}
void push(int val) {
freq[val] += 1;
values[freq[val]].push_back(val);
maxFreq = max(maxFreq, freq[val]);
}
int pop() {
int res = values[maxFreq].back();
values[maxFreq].pop_back();
if (values[maxFreq].empty())
{
values.erase(maxFreq);
maxFreq--;
}
freq[res]--;
return res;
}
};
/**
* Your FreqStack object will be instantiated and called as such:
* FreqStack* obj = new FreqStack();
* obj->push(val);
* int param_2 = obj->pop();
*/