priority_queue语法

  1. #include <queue> // 头文件
  2. priority_queue<int> maxp; // 默认是大顶堆
  3. priority_queue<int, vector<int>, greater<int>> minp; // 小顶堆
  • 大顶堆—-默认 ```cpp

    include

    include

using namespace std;

int main() { priority_queue pq; pq.push(30); pq.push(100); pq.push(25); pq.push(40);

  1. while (!pq.empty()) {
  2. cout << pq.top() << "\t";
  3. pq.pop();
  4. }
  5. // 100、40、30、25
  6. cout << endl;
  7. return 0;

}

  1. - 小顶堆
  2. ```cpp
  3. #include <iostream>
  4. #include <queue>
  5. #include <functional>
  6. using namespace std;
  7. int main()
  8. {
  9. priority_queue<int, vector<int>, greater<int> > pq;
  10. pq.push(30);
  11. pq.push(100);
  12. pq.push(25);
  13. pq.push(40);
  14. while (!pq.empty()) {
  15. cout << pq.top() << "\t";
  16. pq.pop();
  17. }
  18. // 25、30、40、100
  19. cout << endl;
  20. return 0;
  21. }
  • 自定义排序函数 ```cpp // 小的在前 class cmp { public: bool operator()(const int& a, const int& b) {
    1. return a > b;
    } };

// 当然用struct也可 struct mycomparison { bool operator()(const int& a, const int& b) { return a < b; } };

priority_queue, cmp> pri_que;

  1. ---
  2. <a name="ZmOeU"></a>
  3. ### 使用场景
  4. - 解决"第K个数"这类题
  5. ---
  6. <a name="lW1rT"></a>
  7. ### 例题1---[347. 前 K 个高频元素](https://leetcode-cn.com/problems/top-k-frequent-elements/)
  8. ```cpp
  9. class Solution {
  10. public:
  11. // 小顶堆
  12. class mycomparison {
  13. public:
  14. bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {
  15. return lhs.second > rhs.second;
  16. }
  17. };
  18. vector<int> topKFrequent(vector<int>& nums, int k) {
  19. // 要统计元素出现频率
  20. unordered_map<int, int> map; // map<nums[i],对应出现的次数>
  21. for (int i = 0; i < nums.size(); i++) {
  22. map[nums[i]]++;
  23. }
  24. // 对频率排序
  25. // 定义一个小顶堆,大小为k
  26. priority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison> pri_que;
  27. // 用固定大小为k的小顶堆,扫瞄所有频率的数值
  28. for (unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++) {
  29. pri_que.push(*it);
  30. if (pri_que.size() > k) { // 如果堆的大小大于了K,则队列弹出,保证堆的大小一直为k
  31. pri_que.pop();
  32. }
  33. }
  34. // 找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒叙来输出到数组
  35. vector<int> result(k);
  36. for (int i = k - 1; i >= 0; i--) {
  37. result[i] = pri_que.top().first;
  38. pri_que.pop();
  39. }
  40. return result;
  41. }
  42. };

例题2—-692. 前K个高频单词

  1. class Solution {
  2. struct cmp {
  3. bool operator()(const pair<string, int>&a, pair<string, int>& b) {
  4. if (a.second == b.second) return a.first < b.first;
  5. return a.second > b.second;
  6. }
  7. };
  8. public:
  9. vector<string> topKFrequent(vector<string>& words, int k) {
  10. unordered_map<string, int> mp;
  11. for (int i = 0; i < words.size(); ++i) mp[words[i]]++;
  12. priority_queue<pair<string, int>, vector<pair<string, int>>, cmp> pri_q;
  13. for (auto it = mp.begin(); it != mp.end(); ++it) {
  14. pri_q.push(*it);
  15. if (pri_q.size() > k) pri_q.pop();
  16. }
  17. vector<string> result(k);
  18. for (int i = k - 1; i >= 0; --i) {
  19. result[i] = pri_q.top().first;
  20. pri_q.pop();
  21. }
  22. return result;
  23. }
  24. };

例题3—-23. 合并K个升序链表

  1. // 小根堆,根据当前链表值排序。维护入口处为最小值,每次从入口处来串链表,直到队列为空
  2. class Solution {
  3. struct cmp {
  4. bool operator()(ListNode*& a, ListNode*& b) {
  5. return a->val > b->val;
  6. }
  7. };
  8. public:
  9. ListNode* mergeKLists(vector<ListNode*>& lists) {
  10. int len = lists.size();
  11. if (len == 0) return nullptr;
  12. if (len == 1) return lists[0];
  13. ListNode* dummy = new ListNode(-1e5);
  14. ListNode* pre = dummy;
  15. priority_queue<ListNode*, vector<ListNode*>, cmp> pri_q;
  16. for (ListNode* list : lists) {
  17. if (list) pri_q.push(list);
  18. }
  19. while (!pri_q.empty()) {
  20. ListNode* tmp = pri_q.top(); pri_q.pop();
  21. pre->next = tmp;
  22. pre = pre->next;
  23. tmp = tmp->next;
  24. if (tmp) pri_q.push(tmp);
  25. }
  26. ListNode* tmp = dummy;
  27. dummy = dummy->next;
  28. delete(tmp);
  29. return dummy;
  30. }
  31. };