• 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();
     */
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    LeetCode 热题100 11. 盛最多水的容器
    LeetCode 热题100 21. 合并两个有序链表
    LeetCode 热题100 17. 电话号码的字母组合
    LeetCode 热题100 19. 删除链表的倒数第N个节点
    cmake 指定Cpu架构
    [学习笔记]普通平衡树(Splay)
    [原创] RestartPC64中文版v1.0.0.9
    Vue 04 谷歌浏览器配置vue开发者工具
    Vue02 Node下载安装
    springboot框架返回日期值少一天
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10134459.html
Copyright © 2020-2023  润新知