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


🌈个人主页: 南桥几晴秋
🌈C++专栏: 南桥谈C++
🌈C语言专栏: C语言学习系列
🌈Linux学习专栏: 南桥谈Linux
🌈数据结构学习专栏: 数据结构杂谈
🌈数据库学习专栏: 南桥谈MySQL
🌈Qt学习专栏: 南桥谈Qt
🌈菜鸡代码练习: 练习随想记录
🌈git学习: 南桥谈Git

🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 本科在读菜鸡一枚,指出问题及时改正


文章目录


1046.最后一块石头的重量

1046.最后一块石头的重量

cpp 复制代码
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大元素

cpp 复制代码
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个高频单词

cpp 复制代码
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. 数据流的中位数

二分查找+插入排序

cpp 复制代码
#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();
 */

优先队列

cpp 复制代码
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();
 */

相关推荐
蒙奇D索大19 分钟前
【数据结构】图论核心算法解析:深度优先搜索(DFS)的纵深遍历与生成树实战指南
数据结构·算法·深度优先·图论·图搜索算法
让我们一起加油好吗27 分钟前
【基础算法】高精度(加、减、乘、除)
c++·算法·高精度·洛谷
不会敲代码的灵长类33 分钟前
机器学习算法-k-means
算法·机器学习·kmeans
Studying 开龙wu34 分钟前
机器学习有监督学习sklearn实战二:六种算法对鸢尾花(Iris)数据集进行分类和特征可视化
学习·算法·机器学习
鑫鑫向栄1 小时前
[蓝桥杯]缩位求和
数据结构·c++·算法·职场和发展·蓝桥杯
Tony__Ferguson1 小时前
简述八大排序(Sort)
数据结构·算法·排序算法
stormsha1 小时前
MCP架构全解析:从核心原理到企业级实践
服务器·c++·架构
梁下轻语的秋缘1 小时前
每日c/c++题 备战蓝桥杯(P1204 [USACO1.2] 挤牛奶 Milking Cows)
c语言·c++·蓝桥杯
芜湖xin1 小时前
【题解-洛谷】P9422 [蓝桥杯 2023 国 B] 合并数列
算法·队列
鑫鑫向栄1 小时前
[蓝桥杯]外卖店优先级
数据结构·c++·算法·职场和发展·蓝桥杯