LeetCode(295):数据流的中位数 Find Median from Data Stream(Java)

234 篇文章 1 订阅
177 篇文章 0 订阅

2019.9.22 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新)

之前有在剑指Offer上刷过原题:

#数据结构与算法学习笔记#剑指Offer61:数据流中的中位数 + 最大堆 + 最小堆(Java、C/C++)

配合使用一个最大堆和一个最小堆即可,

当元素总数为奇数个时,中位数为最小堆顶;若要添加元素,则经过最小堆过滤后放至最大堆。

当元素总数为偶数个时,中位数为最大堆顶与最小堆顶的平均;若要添加元素,则经过最大堆过滤后放至最小堆。


传送门:数据流的中位数

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.

中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。

例如,[2,3,4] 的中位数是 3

[2,3] 的中位数是 (2 + 3) / 2 = 2.5

设计一个支持以下两种操作的数据结构:

void addNum(int num) - 从数据流中添加一个整数到数据结构中。

double findMedian() - 返回目前所有元素的中位数。

示例:

addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3) 
findMedian() -> 2


import java.util.*;

/**
 *
 * 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.
 * 中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。
 * 例如,[2,3,4] 的中位数是 3. [2,3] 的中位数是 (2 + 3) / 2 = 2.5
 * 设计一个支持以下两种操作的数据结构:
 * void addNum(int num) - 从数据流中添加一个整数到数据结构中。
 * double findMedian() - 返回目前所有元素的中位数。
 *
 */

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

        PriorityQueue<Integer> minheap;
        PriorityQueue<Integer> maxheap;
        boolean oddFlag;

        /** initialize your data structure here. */
        public MedianFinder() {
            minheap = new PriorityQueue<>();
            maxheap = new PriorityQueue<>(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o2 - o1;
                }
            });
            oddFlag = false;
        }

        public void addNum(int num) {
            if(oddFlag){
                minheap.add(num);
                maxheap.add(minheap.poll());
            }else{
                maxheap.add(num);
                minheap.add(maxheap.poll());
            }
            oddFlag = !oddFlag;
        }

        public double findMedian() {
            return minheap.isEmpty() ? 0 : (oddFlag ? minheap.peek() : ((double)maxheap.peek() + minheap.peek()) / 2);
        }
    }
}



#Coding一小时,Copying一秒钟。留个言点个赞呗,谢谢你#

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值