数据流中的中位数

题目:数据流中的中位数

题目描述

如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取数据流,使用GetMedian()方法获取当前读取数据的中位数。

思路

以为是很简单的题目,半分钟AC了.习惯写完看别人的代码,发现自己写的不行。

①最优解:用最大堆、最小堆

#include<bits/stdc++.h> 
using namespace std;
class Solution {
	priority_queue<int, vector<int>, less<int> > p;//从大到小,递减less
	priority_queue<int, vector<int>, greater<int> > q;//递增greater
public:
    void Insert(int num)
    {
         if(q.empty() || num>q.top())
         	q.push(num);
        else p.push(num);
		// p.size() = q.size() || p.size() = q.size()+1才符合条件
		// 最大堆过多
		if(q.size() == p.size()+2){
			p.push(q.top()); q.pop();
		} 
		// 最小堆过多 
		if(p.size() > q.size()){
			q.push(p.top()); p.pop();
		} 
    }

    double GetMedian()
    { 
    	if(q.empty()) return 0.0;
    	return q.size()==p.size() ? (p.top()+q.top())/2.0 : 1.0*q.top();
    }

};
int main(){
	Solution s;
	vector<int> a = {1,2,3,4,5,6};
	for(int i=0;i<a.size();i++)
		s.Insert(a[i]);
	cout<<s.GetMedian();
	
	return 0;
}

 

 

②insert进vector<int> v中,然后排序求中位数就好。面对海量数据的时候效率低。

class Solution {
public:
    vector<int> v;
    void Insert(int num)
    {
        v.push_back(num);
    }

    double GetMedian()
    { 
        double res = 0.0;
        sort(v.begin(), v.end());
        int size = v.size();
        if(size==0) return res;
        if(size%2 == 1){
            res = 1.0 * v[size/2];
        }
        else{
            res = (v[size/2]+v[size/2 -1])/2.0;
        }
        return res;
    }

};

 

③ 有一个狠人,构建一个二叉搜索树,手撸AVL。(就问你服不服)

struct myTreeNode
{
    int val;
    int count;//以此节点为根的树高
    struct myTreeNode* left;
    struct myTreeNode* right;
    myTreeNode(int v) :
        val(v),
        count(1), left(NULL), right(NULL) {}
 
};
 
myTreeNode *root = NULL;
 
class Solution
{
public:
 
    /*计算以节点为根的树的高度
    */
    int totalCount(myTreeNode* node)
    {
        if (node == NULL)
            return 0;
        else
            return node->count;
    }
 
    //左左
    void rotateLL(myTreeNode* &t)
    {
        myTreeNode* k = t->left;
        myTreeNode* tm = NULL;
        while (k->right != NULL)
        {
            k->count--;
            tm = k;
            k = k->right;
             
        }
        if (k != t->left)
        {
            k->left = t->left;
            tm->right = NULL;
        }
        t->left = NULL;
        k->right = t;
 
 
        t->count = totalCount(t->left) + totalCount(t->right) + 1;
        k->count = totalCount(k->left) + t->count + 1;
 
        t = k;
    }
 
    //右右
    void rotateRR(myTreeNode* &t)
    {
        myTreeNode* k = t->right;
        myTreeNode* tm = NULL;
        while (k->left != NULL) {
            k->count--;
            tm = k;
            k = k->left;
             
        }
        if (k != t->right)
        {
            k->right = t->right;
            tm->left = NULL;
        }
             
        t->right = NULL;
        k->left = t;
 
        t->count = totalCount(t->left) + 1;
        k->count = totalCount(k->right)+ t->count + 1;
        t = k;
    }
 
    //左右
    void rotateLR(myTreeNode* &t)
    {
        rotateRR(t->left);
        rotateLL(t);
    }
 
    //右左
    void rotateRL(myTreeNode* &t)
    {
        rotateLL(t->right);
        rotateRR(t);
    }
 
    //插入
    void insert(myTreeNode* &root, int x)
    {
        if (root == NULL)
        {
            root = new myTreeNode(x);
            return;
        }
         
        if (root->val >= x)
        {
            insert(root->left, x);
            root->count = totalCount(root->left)+ totalCount(root->right) + 1;
            if (2 == totalCount(root->left) - totalCount(root->right))
            {
                if (x < root->left->val)
                {
                    rotateLL(root);
                }
                else
                {
                    rotateLR(root);
                }
            }
        }
        else
        {
            insert(root->right, x);
            root->count = totalCount(root->left)+ totalCount(root->right) + 1;
            if (2 == totalCount(root->right) - totalCount(root->left))
            {
                if (x > root->right->val)
                {
                    rotateRR(root);
                }
                else {
                    rotateRL(root);
                }
            }
        }
 
    }
 
    void deleteTree(myTreeNode* root)
    {
        if (root == NULL)return;
        deleteTree(root->left);
        deleteTree(root->right);
        delete root;
        root = NULL;
    }
     
    void Insert(int num)
    {
        insert(root, num);
    }
 
    double GetMedian()
    {
        int lc = totalCount(root->left), rc = totalCount(root->right);
        if ( lc == rc)
            return root->val;
        else
        {
            bool isLeft = lc > rc ;
            myTreeNode* tmp ;
            if (isLeft)
            {
                tmp = root->left;
                while (tmp->right != NULL)
                {
                    tmp = tmp->right;
                }
            }
            else {
                tmp = root->right;
                while (tmp->left != NULL)
                {
                    tmp = tmp->left;
                }
            }
            return (double)(root->val + tmp->val) / 2.0;
        }
    }
 
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值