295. Find Median from Data Stream

Posted ruruozhenhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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?
 

Approach #1: C++. [Heap/priority_queue]

class MedianFinder {
public:
    /** initialize your data structure here. */
    MedianFinder() {
        
    }
    
    void addNum(int num) {
        if (l_.empty() || num < l_.top()) {
            l_.push(num);
        } else {
            r_.push(num);
        }
        
        if (r_.size() > l_.size()) {
            l_.push(r_.top());
            r_.pop();
        } 
        if (l_.size() - r_.size() == 2) {
            r_.push(l_.top());
            l_.pop();
        }
    }
    
    double findMedian() {
        if (l_.size() > r_.size()) return static_cast<double>(l_.top());
        else return static_cast<double>(l_.top() + r_.top()) / 2;
    }
    
private:
    priority_queue<int, vector<int>, less<int>> l_;
    priority_queue<int, vector<int>, greater<int>> r_;
};

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder obj = new MedianFinder();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */

  

Approach #2: C++. [blance binary search tree]

// Author: Huahua
// Running time: 172 ms
class MedianFinder {
public:
    /** initialize your data structure here. */
    MedianFinder(): l_(m_.cend()), r_(m_.cend()) {}
    
    // O(logn)
    void addNum(int num) {
        if (m_.empty()) {
            l_ = r_ = m_.insert(num);
            return;
        }
        
        m_.insert(num);
        const size_t n = m_.size();    
        
        if (n & 1) {
            // odd number
            if (num >= *r_) {         
                l_ = r_;
            } else {
                // num < *r_, l_ could be invalidated
                l_ = --r_;
            }
        } else {
            if (num >= *r_)
                ++r_;
            else
                --l_;
        }
    }
    // O(1)
    double findMedian() {
        return (static_cast<double>(*l_) + *r_) / 2;
    }
private:
    multiset<int> m_;
    multiset<int>::const_iterator l_;  // current left median
    multiset<int>::const_iterator r_;  // current right median
};

@huahuajiang

 

Appraoch #3: Java. [balnce binary search tree]

class MedianFinder {
    private Node root;
    private Node medianLeft;
    private Node medianRight;
    private int size;

    /** initialize your data structure here. */
    public MedianFinder() {
        
    }
    
    public void addNum(int num) {
        if (root == null) {
            root = new Node(num);
            medianLeft = root;
            medianRight = root;
        } else {
            root.addNode(num);
            if (size % 2 == 0) {
                if (num < medianLeft.data) {
                    medianRight = medianLeft;
                } else if (medianLeft.data <= num && num < medianRight.data) {
                    medianLeft = medianLeft.successor();
                    medianRight = medianRight.predecessor();
                } else if (num >= medianRight.data) {
                    medianLeft = medianRight;
                }
            } else {
                if (num < medianLeft.data) {
                    medianLeft = medianLeft.predecessor();
                } else {
                    medianRight = medianRight.successor();
                }
            }
        }
        size++;
    }
    
    public double findMedian() {
        return (medianLeft.data + medianRight.data) / 2.0;
    }
    
    class Node {
        private Node parent;
        private Node left;
        private Node right;
        private int data;
        
        public Node(int data) {
            this.data = data;
        }
        
        public void addNode(int data) {
            if (data >= this.data) {
                if (right == null) {
                    right = new Node(data);
                    right.parent = this;
                } else {
                    right.addNode(data);
                }
            } else {
                if (left == null) {
                    left = new Node(data);
                    left.parent = this;
                } else {
                    left.addNode(data);
                }
            }
        }
        
        public Node predecessor() {
            if (left != null) {
                return left.rightMost();
            }
            
            Node predecessor = parent;
            Node child = this;
            
            while (predecessor != null && child != predecessor.right) {
                child = predecessor;
                predecessor = predecessor.parent;
            }
            
            return predecessor;
        }
        
        public Node successor() {
            if (right != null) {
                return right.leftMost();
            }
            
            Node successor = parent;
            Node child = this;
             while (successor != null && child != successor.left) {
                 child = successor;
                 successor = successor.parent;
             }
            
            return successor;
        }
        
        public Node leftMost() {
            if (left == null) return this;
            return left.leftMost();
        }
        
        public Node rightMost() {
            if (right == null) return this;
            return right.rightMost();
        }
    };
}

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder obj = new MedianFinder();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */

  

 

以上是关于295. Find Median from Data Stream的主要内容,如果未能解决你的问题,请参考以下文章

295. Find Median from Data Stream

295. Find Median from Data Stream

295. Find Median from Data Stream

leetcode295 Find Median from Data Stream

295. Find Median from Data Stream

[leetcode]295. Find Median from Data Stream?????????????????????