Little-Prince

导航

480. 滑动窗口中位数

480. 滑动窗口中位数

中位数是有序序列最中间的那个数。如果序列的大小是偶数,则没有最中间的数;此时中位数是最中间的两个数的平均数。
例如:

 [2,3,4],中位数是 3
 [2,3],中位数是 (2 + 3) / 2 = 2.5

给你一个数组 nums,有一个大小为 k 的窗口从最左端滑动到最右端。窗口中有 k 个数,每次窗口向右移动 1 位。你的任务是找出每次窗口移动后得到的新窗口中元素的中位数,并输出由它们组成的数组。
 
示例:
给出 nums = [1,3,-1,-3,5,3,6,7],以及 k = 3。
窗口位置                      中位数
---------------               -----
[1  3  -1] -3  5  3  6  7       1
 1 [3  -1  -3] 5  3  6  7      -1
 1  3 [-1  -3  5] 3  6  7      -1
 1  3  -1 [-3  5  3] 6  7       3
 1  3  -1  -3 [5  3  6] 7       5
 1  3  -1  -3  5 [3  6  7]      6

 因此,返回该滑动窗口的中位数数组 [1,-1,-1,3,5,6]。
 
提示:

 你可以假设 k 始终有效,即:k 始终小于输入的非空数组的元素个数。
 与真实值误差在 10 ^ -5 以内的答案将被视作正确答案。
 

思路:参考数据流的中位数,维护两个优先队列。对于前k个值,直接加到 big 里面排序,再弹出一半到 small。之后每新进一个元素 innum,判断需要移除的元素 outnum 所在位置,用hash_map 记录 outnum。用 balance 判断两个优先队列是否平衡,balance < 0 说明 small > big,balance > 0 说明 small < big 。每次新进一个元素之前 balance = 0。如果优先队列的 top 为 hash_map 中记录过的元素,删掉优先队列和 hash_map 中的该元素。

 

时间复杂度:O(Nlogk)

 

 

空间复杂度:O(N)

 

代码:

class Solution {
public:
    vector<double> medianSlidingWindow(vector<int>& nums, int k) {
        vector<double> result;
        priority_queue<int, vector<int>, less<int>> small;
        priority_queue<int, vector<int>, greater<int>> big;
        unordered_map<int,int> hash_map;
        int i;
        for(i = 0; i<k; i++)
        {
            big.push(nums[i]);
        }
        for(i = 0; i< k/2; i++)
        {
            small.push(big.top());
            big.pop();
        }
        if(k%2)
            result.push_back(big.top());
        else
            result.push_back(0.5*big.top()+0.5*small.top());
        for(i = k; i < nums.size(); i++)
        {
            int balance = 0;
            int outnum = nums[i-k];
            int innum = nums[i];
            if(!small.empty()&&outnum<=small.top())
                {
                     balance++; 
                }
            else 
                {
                     balance--;
                }
                 hash_map[outnum]++;
                
            if(!small.empty()&&innum<small.top())
            {
                small.push(innum);
                balance--;
            }
            else
            {
                big.push(innum);
                balance++;
            }
            
            if(balance>0)
            {
                small.push(big.top());
                big.pop();
                balance--;
            }
            else if(balance<0)
            {
                big.push(small.top());
                small.pop();
                balance++;
            }

            while(!small.empty()&&hash_map[small.top()])
            {
                hash_map[small.top()]--;
                small.pop();
                
                
            }
            while(!big.empty()&&hash_map[big.top()])
            {
                hash_map[big.top()]--;
                big.pop();
            }
        if(k%2)
            result.push_back(big.top());
        else
            result.push_back(0.5*big.top()+0.5*small.top());
        }
        return result;
    }
};

 

posted on 2020-08-07 23:20  Little-Prince  阅读(207)  评论(0)    收藏  举报