编程语言
首页 > 编程语言> > 算法笔记-优先队列

算法笔记-优先队列

作者:互联网

数据流中的中位数

#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

class MedianFinder {
public:
    /** initialize your data structure here. */
    MedianFinder() {
    }
    
    void addNum(int num) {
        left.push(num);
        if (!right.empty() && left.top() > right.top()) {
            int leftMax = left.top();
            int rightMin = right.top();
            left.pop();
            right.pop();
            left.push(rightMin);
            right.push(leftMax);
        }
        if (left.size() - right.size() > 1) {
            right.push(left.top());
            left.pop();
        }
    }
    
    double findMedian() {
        if (left.size() > right.size()) {
            return left.top();
        }
        return (left.top() + right.top()) / 2.0;
    }
private:
    priority_queue<int, vector<int>, less<int>> left;
    priority_queue<int, vector<int>, greater<int>> right;
};
/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder* obj = new MedianFinder();
 * obj->addNum(num);
 * double param_2 = obj->findMedian();
 */

查找和最小的k对数字

#include <math.h>
class Solution {
public:
    typedef struct PairSum {
        int x;
        int y;
        int sum;
        bool operator < (const PairSum& p) const {
            return sum < p.sum;
        }
    } PairSum;
    vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
        priority_queue<PairSum, vector<PairSum>> maxHeap;
        for (int i = 0; i < min(k, (int)nums1.size()); ++i) {
            for (int j = 0; j < min(k, (int)nums2.size()); ++j) {
                if (maxHeap.size() < k) {
                    maxHeap.push({nums1[i], nums2[j], nums1[i] + nums2[j]});
                    continue;
                }
                if (maxHeap.top().sum > nums1[i] + nums2[j]) {
                    maxHeap.pop();
                    maxHeap.push({nums1[i], nums2[j], nums1[i] + nums2[j]});
                }
            }
        }
        vector<vector<int>> res;
        while (!maxHeap.empty()) {
            res.push_back({maxHeap.top().x, maxHeap.top().y});
            maxHeap.pop();
        }
        return res;
    }
};

丑数

class Solution {
public:
    int nthUglyNumber(int n) {
        priority_queue<long long, vector<long long>, greater<long long>> candidates;
        vector<int> sources{2, 3, 5};
        long long ans = 1;
        set<long long> visited{1};
        for (int i = 1; i < n; ++i) {
            for (int j = 0; j < sources.size(); ++j) {
                if (visited.count(ans * sources[j]) == 0) {
                    candidates.push(ans * sources[j]);
                    visited.insert(ans * sources[j]);
                }
            }
            ans = candidates.top();
            candidates.pop();
        }
        return ans;
    }
};

天际线问题

typedef struct Node{
    int x;
    int h;
    int type;
}Node;
class Solution {
public:
    vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
        vector<Node> nodes;
        for (const auto& pos: buildings) {
            int x = pos[0];
            int y = pos[1];
            int h = pos[2];
            nodes.push_back({x, h, -1});
            nodes.push_back({y, h, 1});
        }
        sort(nodes.begin(), nodes.end(),
             [=](const Node& a, const Node& b)
             {
                 if (a.x == b.x) {
                     return a.h * a.type < b.h * b.type;
                 }
                 return a.x < b.x;
             });
        vector<vector<int>> res;
        multiset<int> maxHeap{0};
        for (int i = 0; i < nodes.size(); ++i) {
            int x = nodes[i].x;
            int h = nodes[i].h;
            int type = nodes[i].type;
            // left
            if (type == -1) {
                if (h > *maxHeap.rbegin()) res.push_back({x, h});
                maxHeap.insert(h);
                continue;
            }
            // right
            if (type == 1) {
                maxHeap.erase(maxHeap.find(h));
                if (h > *maxHeap.rbegin()) {
                    res.push_back({x, *maxHeap.rbegin()});
                }
            }
        }
        return res;
    }
};

标签:right,队列,top,maxHeap,笔记,int,算法,push,left
来源: https://blog.csdn.net/rao_raoW/article/details/120344560