295.Find Median from Data Stream

题目描述

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.

For example, [2,3,4], the median is 3

[2,3], the median is (2 + 3) / 2 = 2.5

Design a data structure that supports the following two operations:

  • void addNum(int num) - Add a integer number from the data stream to the data structure.

  • double findMedian() - Return the median of all elements so far.

Example

addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3)
findMedian() -> 2

Follow up

1.If all integer numbers from the stream are between 0 and 100, how would you optimize it?

2.If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it?

难度系数

Hard

解法一:创建数组,利用二分查找

class MedianFinder {
public:
    MedianFinder() {
    
    }
    // Adds a number into the data structure.
    void addNum(int num) {
        int left = 0, right = nums.size()-1;
         //注意循环的条件是小于等于,在=条件时,仍需判断,在当前值的左边还是右边插入
        while(left <= right)
        {
            int mid = (left+right)/2;
            if(nums[mid] < num) left = mid+1;
            else right = mid-1;
        }
        nums.insert(nums.begin()+left, num);
    }
 
    // Returns the median of current data stream
    double findMedian() {
        int n = nums.size();
        if(n%2==1) return nums[n/2];
        return (nums[n/2-1] + nums[n/2])/2.0;
    }
private:
    vector<int> nums;
};

 

解法二:分别使用一个大顶堆和小顶堆,将数组分为两半,大顶堆的堆顶元素和小顶堆的对顶元素为中间元素

class MedianFinder {
private:
    /*
     * 创建一个大顶堆和一个小顶堆,大顶堆保存数据左半部分,小顶堆保存右边部分*/
    //小顶堆
    priority_queue<int, vector<int>, greater<int> > small;
    //默认大顶堆
    priority_queue<int> large;
    void balance_heaps() {
        if (small.size() > large.size() + 1) {
            int moved = small.top();
            small.pop();
            large.push(moved);
        } else if (large.size() > small.size() + 1) {
            int moved = large.top();
            large.pop();
            small.push(moved);
        }
    }
public:
    /** initialize your data structure here. */
    MedianFinder() {
        small.push(numeric_limits<int>::max()); // dummy values for corner case
        large.push(numeric_limits<int>::min());
    }
​
    void addNum(int num) {
        if (num > small.top()) {
            small.push(num);
        } else {
            large.push(num);
        }
        balance_heaps();
    }
​
    double findMedian() {
        if (small.size() == 1 && large.size() == 1)
            return 0; // No numbers
        if (small.size() == large.size()) {//偶数的情况
            return (small.top() + large.top()) / 2.0;
        } else if (small.size() > large.size()) {//奇数的情况
            return small.top();
        } else {
            return large.top();
        }
    }
​
};
 
/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder* obj = new MedianFinder();
 * obj->addNum(num);
 * double param_2 = obj->findMedian();
 */

 

原文地址:https://www.cnblogs.com/AntonioSu/p/12735120.html