Skip to content

Latest commit

 

History

History

295

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> sm;
    priority_queue<int, vector<int>, greater<>> gt;
public:
    MedianFinder() {}
    void addNum(int num) {
        if (gt.size() && num > gt.top()) {
            gt.push(num);
            if (gt.size() > sm.size()) {
                sm.push(gt.top());
                gt.pop();
            }
        } else {
            sm.push(num);
            if (sm.size() > gt.size() + 1) {
                gt.push(sm.top());
                sm.pop();
            }
        }
    }
    double findMedian() {
        return sm.size() > gt.size() ? sm.top() : ((double)sm.top() + gt.top()) / 2;
    }
};

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> sm;
    priority_queue<int, vector<int>, greater<>> gt;
public:
    MedianFinder() {}
    void addNum(int num) {
        sm.push(num);
        gt.push(sm.top());
        sm.pop();
        if (gt.size() > sm.size()) {
            sm.push(gt.top());
            gt.pop();
        }
    }
    double findMedian() {
        return sm.size() > gt.size() ? sm.top() : ((double)sm.top() + gt.top()) / 2;
    }
};

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.;
    }
};