求变化序列的中位数

本文介绍了如何在数据流中求中位数的算法,具体通过两个案例——LC 295 和 LC 480,讲解了使用大根堆和小根堆的数据结构来实现。在LC 295中,讨论了如何维护数据流的中位数;而在LC 480中,由于涉及到滑动窗口,引入了multiset来处理窗口外元素的移除问题。
摘要由CSDN通过智能技术生成

求变化序列的中位数

一、数据流(LC 295)

1.题目
The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.

For example, for arr = [2,3,4], the median is 3.
For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5.
Implement the MedianFinder class:

MedianFinder() initializes the MedianFinder object.
void addNum(int num) adds the integer num from the data stream to the data structure.
double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted.
2.思路
用大根堆和小根堆维护较大的一堆元素和较小的一堆元素。
3.代码

class MedianFinder {
public:
    priority_queue<int> bottom;
    priority_queue<int, vector<int>, greater<int>> tt;
    int count;
    /** initialize your data structure here. */
    MedianFinder() {
        count = 0;
    }

    void addNum(int num) {
        if (!count || num < bottom.top()) {
            bottom.push(num);
            if (bottom.size() > tt.size() + 1) {
                tt.push(bottom.top());
                bottom.pop();
            }
        }
        else {
            tt.push(num);
            if (tt.size() >= bottom.size() + 1) {
                bottom.push(tt.top());
                tt.pop();
            }
        }
        count++;
    }

    double findMedian() {
        if (count % 2 == 0) return ((double)tt.top() + bottom.top()) / 2;
        else return bottom.top();
    }
};

二、滑动窗口(LC 480)

1.题目
求滑动窗口中位数
2.思路
用大根堆和小根堆维护较大的一堆元素和较小的一堆元素,但是这里需要移除窗口外的元素,用priority queue做不了,因此用multiset(有序,方便移除指定元素)模拟大根堆和小根堆。
3.代码

class Solution {
public:
    multiset<int> left, right;
    int k;
    double getmedium() {
        if (k % 2 != 0) {
            return *right.begin();
        }
        else {
            return ((double)*right.begin() + *left.rbegin()) / 2;
        }
    }
    vector<double> medianSlidingWindow(vector<int>& nums, int _k) {
        k = _k;
        vector<double> res;
        for (int i = 0; i < k; i++) right.insert(nums[i]);
        for (int i = 0; i < k / 2; i++) {
            left.insert(*right.begin());
            right.erase(right.begin());
        }
        res.push_back(getmedium());
        for (int i = k; i < nums.size(); i++) {
            int x = nums[i], y = nums[i - k];
            if (x >= *right.begin()) right.insert(x);
            else left.insert(x);
            if (y >= *right.begin()) right.erase(right.find(y));
            else left.erase(left.find(y));
            //更新两个堆,使两边个数相差不超过1
            while (left.size() > right.size()) {
                right.insert(*left.rbegin());
                left.erase(left.find(*left.rbegin()));
            }
            while (right.size() > left.size() + 1) {
                left.insert(*right.begin());
                right.erase(right.begin());
            }
            res.push_back(getmedium());
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值