nikunjsoni

895

Apr 9th, 2021
71
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class FreqStack {
  2. public:
  3.     unordered_map<int, int> freq;
  4.     unordered_map<int, stack<int> > m;
  5.     int maxFreq;
  6.     FreqStack() {
  7.         maxFreq = 0;
  8.     }
  9.    
  10.     void push(int val) {
  11.         freq[val]++;
  12.         m[freq[val]].push(val);
  13.         maxFreq = max(maxFreq, freq[val]);
  14.     }
  15.    
  16.     int pop() {
  17.         int tmp = m[maxFreq].top();
  18.         m[maxFreq].pop();
  19.         if(m[maxFreq].empty()) maxFreq--;
  20.         freq[tmp]--;
  21.         return tmp;
  22.     }
  23. };
  24.  
  25. /**
  26.  * Your FreqStack object will be instantiated and called as such:
  27.  * FreqStack* obj = new FreqStack();
  28.  * obj->push(val);
  29.  * int param_2 = obj->pop();
  30.  */
RAW Paste Data