🔥✅Best Code in C++ || MinHeap💯
Please Upvote if u liked my Solution\uD83E\uDD17\n# Complexity\n- Time complexity:\nO(nlogk)\n\n- Space complexity:\nO(n)\n\n# Code\n\nclass Solution {\npublic:
## Please Upvote if u liked my Solution\uD83E\uDD17\n# Complexity\n- Time complexity:\nO(nlogk)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\npublic:\n    int miceAndCheese(vector<int>& reward1, vector<int>& reward2, int k) {\n        int n=reward1.size();\n        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n        for(int i=0;i<n;i++){\n            int diff=reward1[i] - reward2[i];\n            pq.push({diff,i});\n            if(pq.size()>k)\n                pq.pop();\n        }\n        int ans=0;\n        vector<int> mark(n);\n        while(pq.size()){\n            int ind=pq.top().second;\n            ans += reward1[ind];\n            pq.pop();\n            mark[ind]=1;\n        }\n        for(int i=0;i<n;i++){\n            if(mark[i]==0)\n                ans += reward2[i];\n        }\n        return ans;\n    }\n};\n```\n![e2515d84-99cf-4499-80fb-fe458e1bbae2_1678932606.8004954.png](https://assets.leetcode.com/users/images/397c8e3f-b237-4fc9-8053-c35a27b0b49f_1680408169.4994588.png)\n
['Greedy', 'Heap (Priority Queue)', 'C++']