前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【算法刷题指南】优先级队列

【算法刷题指南】优先级队列

作者头像
南桥
发布2024-12-03 08:18:33
发布2024-12-03 08:18:33
7800
代码可运行
举报
文章被收录于专栏:南桥谈编程
运行总次数:0
代码可运行

1046.最后一块石头的重量

1046.最后一块石头的重量

代码语言:javascript
代码运行次数:0
复制
class Solution {
public:
    int lastStoneWeight(vector<int>& stones) {
        priority_queue<int> heap;
        for(auto x:stones) heap.push(x);
        while(heap.size()>1)
        {
            int a=heap.top();
            heap.pop();
            int b=heap.top();
            heap.pop();

            if(a>b) heap.push(a-b);
        }
        return heap.size()?heap.top():0;
    }
};

703.数据流中的第k大元素

703.数据流中的第k大元素

代码语言:javascript
代码运行次数:0
复制
class KthLargest {
    priority_queue<int,vector<int>,greater<int>> heap;
    int _k;
public:
    KthLargest(int k, vector<int>& nums) {
        _k=k;
        for(auto x:nums) 
        {
            heap.push(x);
            if(heap.size()>_k) heap.pop();
        }
    }
    
    int add(int val) {
        heap.push(val);
        if(heap.size()>_k) heap.pop();
        return heap.top();
    }
};

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest* obj = new KthLargest(k, nums);
 * int param_1 = obj->add(val);
 */

692.前K个高频单词

692.前K个高频单词

代码语言:javascript
代码运行次数:0
复制
class Solution {
    typedef pair<string,int> PSI;
    struct cmp
    {
        bool operator()(const PSI& a,const PSI& b)
        {
            if(a.second==b.second) return a.first<b.first;
            return a.second>b.second;

        }
    };
public:
    vector<string> topKFrequent(vector<string>& words, int k) {
        unordered_map<string,int> hash;
        for(auto &s:words) hash[s]++;
        priority_queue<PSI,vector<PSI>,cmp> heap;
        for(auto &pis:hash)
        {
            heap.push(pis);
            if(heap.size()>k) heap.pop();
        }
        vector<string> ans(k);
        for(int i=k-1;i>=0;i--)
        {
            ans[i]=heap.top().first;
            heap.pop();
        }
        return ans;
    }
};

295. 数据流的中位数

295. 数据流的中位数

二分查找+插入排序

代码语言:javascript
代码运行次数:0
复制
#include<algorithm>
#include<vector>
class MedianFinder {
public:
    MedianFinder() {
        
    }
    vector<int> newarr;
    void addNum(int num) {
        auto it=lower_bound(newarr.begin(),newarr.end(),num);
        newarr.insert(it,num);
    }
    
    double findMedian() {
        int n=newarr.size();
        if(n%2==1) return newarr[n/2];
        else return  (newarr[n / 2 - 1] + newarr[n / 2]) / 2.0;
    }
};

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder* obj = new MedianFinder();
 * obj->addNum(num);
 * double param_2 = obj->findMedian();
 */

优先队列

代码语言:javascript
代码运行次数:0
复制
class MedianFinder {
    priority_queue<int> left;
    priority_queue<int,vector<int>,greater<int>> right;

public:
    MedianFinder() {}
    
    void addNum(int num) {
        if(left.size()==right.size())
        {
            if(left.empty()||num<left.top())
            {
                left.push(num);
            }
            else
            {
                right.push(num);
                left.push(right.top());
                right.pop();
            }
        }   
        else
        {
            if(num<=left.top())
            {
                left.push(num);
                right.push(left.top());
                left.pop();
            }
            else
            {
                right.push(num);
            }
        } 
    }
    
    double findMedian() {
        if(left.size()==right.size()) return (left.top()+right.top())/2.0;
        else return left.top();
    }
};

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder* obj = new MedianFinder();
 * obj->addNum(num);
 * double param_2 = obj->findMedian();
 */
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-12-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1046.最后一块石头的重量
  • 703.数据流中的第k大元素
  • 692.前K个高频单词
  • 295. 数据流的中位数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档