算法笔记-优先队列/扫描线

数据流中的中位数

#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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值