[C++] Priority Queue + Map | Comments for Easy Understanding 📌
Before you see this solution, I would highly suggest you to try this below problem :\n\uD83D\uDC49  https://leetcode.com/problems/top-k-frequent-elements/\n\nIf
**Before you see this solution, I would highly suggest you to try this below problem :**\n\uD83D\uDC49  ***https://leetcode.com/problems/top-k-frequent-elements/***\n\n**If you solved the above problem, that\'s great. You can try again this problem and if still can\'t, then continue seeing this solution.** \n**In case you couldn\'t solve above link problem, see the below link solution and then see this solution, everything will become crystal-clear to you.**\n\uD83D\uDC49 ***https://leetcode.com/problems/top-k-frequent-elements/discuss/1352094/c-8ms-9932-faster-priority-queue-comments-for-easy-understanding***\n\n\n\n![image](https://assets.leetcode.com/users/images/f6bf1b3b-bc3c-4f0f-9c59-a2690006eaa5_1626855499.9966238.png)\n\n```\nclass Solution {\npublic:\n    class compare_fun\n    {    \n        public:\n        // Since as you can see we have to sort decreasing order if frequency of two elements\n        // is same, so we need *Custom Comparator Function* which is this \uD83D\uDC47\n        bool operator()(pair<int, int> p1, pair<int, int> p2) \n        {    \n            if(p1.first==p2.first)\n                return p1.second < p2.second;\n            return p1.first > p2.first;\n        }\n    };\n    \n    vector<int> frequencySort(vector<int>& nums) {\n        \n        //Creating map of array elements with their count\n        unordered_map<int, int> mp;\n        for(auto x: nums)\n            mp[x]++;\n        \n        // *Min Heap*\n        priority_queue<pair<int,int>, vector<pair<int,int>>, compare_fun> minH;\n        vector<int> v;\n        \n        //Traversing through map and pushing it in heap\n        for(auto x: mp){\n            minH.push(make_pair(x.second, x.first)); // Making pair of count and the number\n        }\n        \n        //Pushing the heap elements in vector multiplied with it\'s frequency\n        while(minH.size() > 0){\n            for(int i=0; i<minH.top().first; i++)\n                v.push_back(minH.top().second);\n            minH.pop();\n        }\n        \n        return v;\n    }\n};\n```
['Heap (Priority Queue)']