• 295. 数据流的中位数


    295. 数据流的中位数
    中位数是有序整数列表中的中间值。如果列表的大小是偶数,则没有中间值,中位数是两个中间值的平均值。
    
    例如 arr = [2,3,4] 的中位数是 3 。
    例如 arr = [2,3] 的中位数是 (2 + 3) / 2 = 2.5 。
    实现 MedianFinder 类:
    
    MedianFinder() 初始化 MedianFinder 对象。
    
    void addNum(int num) 将数据流中的整数 num 添加到数据结构中。
    
    double findMedian() 返回到目前为止所有元素的中位数。与实际答案相差 10-5 以内的答案将被接受。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    list 存储 超时

    class MedianFinder {
    
        List<Integer> list = new ArrayList<>();
    
        public MedianFinder() {
    
        }
        
        public void addNum(int num) {
            int i = list.size();
            while (i > 0) {
                if (num < list.get(i-1)) {
                    i-=1;
                } else {
                    break;
                }
            }
            if (i == list.size()) {
                list.add(num);
            } else {
                list.add(i, num);
            }
        }
        
        public double findMedian() {
            int l = list.size();
            if (l % 2 == 1) {
                return list.get(l/2);
            }
            return (list.get(l/2) + list.get(l/2-1)) / 2.0D;
        }
    }
    
    /**
     * Your MedianFinder object will be instantiated and called as such:
     * MedianFinder obj = new MedianFinder();
     * obj.addNum(num);
     * double param_2 = obj.findMedian();
     */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    最大最小堆

    class MedianFinder {
    
        PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>((a, b) -> b-a);
        PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>((a, b) -> a-b);
    
        public MedianFinder() {
    
        }
        
        public void addNum(int num) {
            if (maxHeap.isEmpty() || num < maxHeap.peek()) {
                maxHeap.offer(num);
                if (minHeap.size() + 1 < maxHeap.size()){
                    minHeap.offer(maxHeap.poll());
                }
            }  else {
                minHeap.offer(num);
                if (minHeap.size() > maxHeap.size()) {
                    maxHeap.offer(minHeap.poll());
                }
            }
        }
        
        public double findMedian() {
            if (maxHeap.size() > minHeap.size()) {
                return maxHeap.peek();
            }
            return (minHeap.peek() + maxHeap.peek()) / 2.0D;
        }
    }
    
    /**
     * Your MedianFinder object will be instantiated and called as such:
     * MedianFinder obj = new MedianFinder();
     * obj.addNum(num);
     * double param_2 = obj.findMedian();
     */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    【Linux网络编程】 I/O复用
    2022/08/08 day05:Jedis
    暑假加餐|有钱人和你想的不一样(第四天)+多目标萤火虫优化算法(Matlab代码)
    Sping.事务的传播特性
    Python:if判断--综合案例练习:石头剪刀布
    阿里云:加大NoSQL数据库软硬件一体化技术自研
    微信小程序获取当前日期时间
    pip某些包发生SSL错误
    【华为OD机试真题 JS】找车位
    c++特性之auto
  • 原文地址:https://blog.csdn.net/zslngu/article/details/137846577