Skip to content

Latest commit

 

History

History
 
 

295. Find Median from Data Stream

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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?

Related Topics:
Heap, Design

Similar Questions:

Solution 1. Heap

// OJ: https://leetcode.com/problems/find-median-from-data-stream/
// Author: github.com/lzl124631x
// Time:
//      MedianFinder: O(1)
//      addNum: O(logN)
//      findMedian: O(1)
// Space: O(N)
class MedianFinder {
    priority_queue<int> small;
    priority_queue<int, vector<int>, greater<int>> great;
public:
    MedianFinder() {}
    
    void addNum(int num) {
        if (great.size() && num >= great.top()) {
            great.push(num);
            if (great.size() > small.size() + 1) {
                small.push(great.top());
                great.pop();
            }
        } else {
            small.push(num);
            if (small.size() > great.size() + 1) {
                great.push(small.top());
                small.pop();
            }
        }
    }
    
    double findMedian() {
        return small.size() == great.size() ? (small.top() + great.top()) / 2. : (small.size() > great.size() ? small.top() : great.top());
    }
};

Or

// OJ: https://leetcode.com/problems/find-median-from-data-stream/
// Author: github.com/lzl124631x
// Time:
//      MedianFinder: O(1)
//      addNum: O(logN)
//      findMedian: O(1)
// Space: O(N)
class MedianFinder {
    priority_queue<int> small;
    priority_queue<int, vector<int>, greater<int>> great;
public:
    MedianFinder() {}
    void addNum(int num) {
        great.push(num);
        small.push(great.top());
        great.pop();
        if (small.size() > great.size()) {
            great.push(small.top());
            small.pop();
        }
    }
    double findMedian() {
        return small.size() == great.size() ? (small.top() + great.top()) / 2. : (small.size() > great.size() ? small.top() : great.top());
    }
};

Solution 2. Multiset

// OJ: https://leetcode.com/problems/find-median-from-data-stream/
// Author: github.com/lzl124631x
// Time:
//      MedianFinder: O(1)
//      addNum: O(logN)
//      findMedian: O(1)
// Space: O(N)
class MedianFinder {
    multiset<int> s;
    multiset<int>::iterator mid;
public:
    MedianFinder() {}
    void addNum(int num) {
        int N = s.size() + 1;
        s.insert(num);
        if (N == 1) mid = s.begin();
        else if (num < *mid) mid = N % 2 ? mid : prev(mid);
        else mid = N % 2 ? next(mid) : mid;
    }
    double findMedian() {
        return s.size() % 2 ? *mid : (*mid + *next(mid)) / 2.;
    }
};