优先级队列(堆)

1、最后一块石头的重量

1046. 最后一块石头的重量 - 力扣(LeetCode)

class Solution 
{
public:
    int lastStoneWeight(vector<int>& stones) 
    {
        priority_queue<int> pq(stones.begin(),stones.end());
        while(pq.size() > 1)
        {
            int y = pq.top(); pq.pop();
            int x = pq.top(); pq.pop();
            if(x == y){}
            else
            {
                y = y-x;
                pq.push(y); 
            }
        }
        if(pq.size() == 0) return 0;
        return pq.top();
    }
};

2、数据流中的第k大元素

703. 数据流中的第 K 大元素 - 力扣(LeetCode)

Top K问题

class KthLargest 
{
    priority_queue<int,vector<int>,greater<int>> pq;//小根堆
    int _k;
public:
    KthLargest(int k, vector<int>& nums) 
    {
        _k = k;
        for(auto x : nums)
        {
            pq.push(x);
            if(pq.size() > _k) pq.pop();
        }
    }
    
    int add(int val) 
    {
        pq.push(val);
        if(pq.size() > _k) pq.pop();
        return pq.top();
    }
};

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

3、前K个高频单词

692. 前K个高频单词 - 力扣(LeetCode)

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) 
    {
        //1、统计每个单词出现的频次
        unordered_map<string,int> hash;
        for(auto s : words)
            hash[s]++;

        //2、创建大小为k的堆
        priority_queue<PSI,vector<PSI>,cmp> heap;
        
        //3、TopK的主逻辑
        for(auto& psi : hash)
        {
            heap.push(psi);
            if(heap.size() > k) heap.pop();
        }

        //4、提取结果
        vector<string> res(k);
        for(int i = k-1;i>=0;i--)
        {
            res[i] = heap.top().first;
            heap.pop();
        }
        return res;
    }
};

4、数据流的中位数

295. 数据流的中位数 - 力扣(LeetCode)

解法一:直接sort,然后求中位数(时间复杂度太高,每次求中位数都要sort,会超时)

class MedianFinder 
{
    vector<int> nums;
public:
    MedianFinder() 
    {

    }
    
    void addNum(int num) 
    {
        nums.push_back(num);
    }
    
    double findMedian() 
    {
        sort(nums.begin(),nums.end());
        int n = nums.size();
        if(n % 2 == 1)
            return nums[n/2];
        return ((double)nums[n/2] + (double)nums[n/2 -1])/2;
    }
};

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

解法二:插入排序的思想

如果add调用很多次的话,时间复杂度也很高,但这题能过。

class MedianFinder 
{
    vector<int> nums;
public:
    MedianFinder() 
    {

    }
    
    void addNum(int num) 
    {
        if(nums.size() == 0) nums.push_back(num);
        else
        {
            auto it = nums.begin();
            while(it != nums.end() && *it < num)
                it++;
            nums.insert(it,num);
        }        
    }
    
    double findMedian() 
    {
        int n = nums.size();
        if(n % 2 == 1)
            return nums[n/2];
        return ((double)nums[n/2] + (double)nums[n/2 -1])/2;
    }
};

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

解法三:大小堆来维护数据流的中位数

大根堆:记录前一半的最小元素。

小根堆:记录后一半的最大元素。

中位数 = (小根堆top + 大根堆top)/2;

class MedianFinder
{
    priority_queue<int> left_heap;//前k个最小元素,大堆,left.top就是左边小的数里最大的
    priority_queue<int,vector<int>,greater<int>> right_heap;//前k个最大元素,小堆,right.top就是右边大的数里最小的
    int left_n = 0, right_n = 0;
    //规定,如果一共有奇数个数,那规定left_n == right_n + 1;
    //如果是偶数个,则left_n == right_n
    //添加数的时候,就要保证这个特性--”两边数据个数均分“

    //添加数的细节:
    //1、如果left_n == right_n
    //(1)left_top >= num(左堆的堆顶元素>=num) || left_n == 0,直接进入左堆,left_n++
    //(2)left_top < num(左堆的堆顶元素<num),num进入右堆,此时left_n == right_n-1,此时要将right.top放进左堆,然后,left_n++,left_n == right_n + 1
    //2、如果left_n == right_n + 1
    //(1)left_top >= num,直接放入左堆,left_n == right_n +2,此时将left.top放入右堆,然后right_n++,left_n == right_n
    //(2)left_top <num,直接放入右堆,right_n++
public:
    MedianFinder() 
    {

    }
    
    void addNum(int num) 
    {
        if(left_n == right_n)
        {
            if(left_n == 0 || left_heap.top() >= num)
            {
                left_heap.push(num);
                left_n++;
            }
            else if(left_heap.top() < num)
            {
                right_heap.push(num);
                int top = right_heap.top(); right_heap.pop();
                left_heap.push(top);
                left_n++;
            }
        }
        else if(left_n == right_n + 1)
        {
            if(left_heap.top() >= num)
            {
                left_heap.push(num);
                int top = left_heap.top(); left_heap.pop();
                right_heap.push(top);
                right_n++;
            }
            else if(left_heap.top() < num)
            {
                right_heap.push(num);
                right_n++;
            }
        }
    }
    
    double findMedian() 
    {
        if(left_n == right_n)
            return (left_heap.top() + right_heap.top()) / 2.0;
        return left_heap.top();
    }
};

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

 

 

 

 

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夹心宝贝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值