Java教程

算法笔记-优先队列

本文主要是介绍算法笔记-优先队列,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

数据流中的中位数

#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;
    }
};
这篇关于算法笔记-优先队列的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!