Maximum Frequency Stack LeetCode solution in C++
1class FreqStack {2public:3unordered_map<int, int> ump;4unordered_map<int, stack<int>> ump_st;56int cap = 1;78FreqStack() {9ump.clear();10ump_st.clear();11}1213void push(int val) {14// increasing the count15if (ump.find(val) != ump.end()) {16ump[val]++;17} else {18ump[val] = 1;19}2021// update the highest level22if (cap < ump[val]) {23cap = ump[val];24}2526// push the elements in the stack where it belongs as per height27ump_st[ump[val]].push(val);28}2930int pop() {31int val = ump_st[cap].top();32ump_st[cap].pop();3334if (ump_st[cap].size() == 0) {35cap--;36}3738ump[val]--;3940if (ump[val] == 0) {41ump.erase(val);42}4344return val;45}46};