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.

Examples:

[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.

For example:

add(1)
add(2)
findMedian() -> 1.5
add(3)
findMedian() -> 2 设计数据结构存储数据流并能找出数据流的中位数。
思路:首先找中位数是需要对数据流进行排序的。但是这里不是一次给出所有数据,而是逐渐累加。因此要维护一个有序数列。
首先想到的是Java中的SortedSet可以维护有序集,但是在获取中位数的时候必须要转换成数组,才能直接获取到中位数,时间复杂度较大。超时。
class MedianFinder {

    private int count;
private int sum;
private java.util.SortedSet<Integer> set; public MedianFinder() {
set = new TreeSet();
} // Adds a number into the data structure.
public void addNum(int num) {
set.add(num);
} // Returns the median of current data stream
public double findMedian() {
Integer[] list = set.toArray(new Integer[0]);
int size = set.size();
double res = 0.0;
if(size % 2 == 0) {
res = (double)(list[size/2] + list[size/2 - 1]) / 2.0;
}
else {
res = (double)list[size/2];
}
return res;
}
}; // Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf = new MedianFinder();
// mf.addNum(1);
// mf.findMedian();

然后看到网上有用优先队列实现的,思路是这样的,维护两个优先队列,其实内部是用堆实现的。维护一个大顶堆,一个小顶堆。分别存储数据流中较大的一般和较小的一半。如果数据流的总数是奇数那么大顶堆中的个数要多一个,这样一来在获取中位数的时候,对于数据流总数是奇数的情况直接返回大顶堆堆顶,对于数据流总数是偶数的情况返回两个堆顶的平均数。最重要的是要维护两个堆的大小。在优先队列中offer,poll时间复杂度为O(logn) peek时间复杂度为O(1),所以addNum的时间复杂度为O(logn),findMedian时间复杂度为O(1)。

public class MedianFinder {

    private Queue<Integer> maxHeap; //大顶堆
private Queue<Integer> minHeap; //小顶堆 public MedianFinder() {
maxHeap = new PriorityQueue<Integer>(11,Collections.reverseOrder());
minHeap = new PriorityQueue<Integer>();
} public void addNum(int num) {
//插入大顶堆
if(maxHeap.size()==0 || maxHeap.peek()>=num) {
maxHeap.offer(num);
if(maxHeap.size()-1 > minHeap.size()) {
minHeap.offer(maxHeap.poll());
}
}
//插入小顶堆
else if(minHeap.size()==0 || minHeap.peek() < num) {
minHeap.offer(num);
if(minHeap.size() > maxHeap.size()) {
maxHeap.offer(minHeap.poll());
}
}
//两者之间,先考虑大顶堆。
else {
if(maxHeap.size() <= minHeap.size()) {
maxHeap.offer(num);
}
else {
minHeap.offer(num);
}
} } public double findMedian() {
if(maxHeap.size() == minHeap.size()) {
return (double)(maxHeap.peek() + minHeap.peek()) / 2.0;
}
else {
return (double)maxHeap.peek();
}
} }


LeetCode——Find Median from Data Stream的更多相关文章

  1. [LeetCode] Find Median from Data Stream

    Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of t ...

  2. [LeetCode] 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 ...

  3. [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)

    295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...

  4. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  5. leetcode@ [295]Find Median from Data Stream

    https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...

  6. [LeetCode] 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 ...

  7. 【LeetCode】295. Find Median from Data Stream 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 日期 题目地址:https://le ...

  8. [leetcode]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 ...

  9. LeetCode OJ: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 ...

随机推荐

  1. Activiti 查看流程图

    package com.mycom.processDefinition; import java.io.File; import java.io.IOException; import java.io ...

  2. eclipse 引用自己开发的模块

    这样就可以 生成的是LIB 工程需要设置“Is Library”

  3. python3 slice

    Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32Type & ...

  4. Entity Framework 6.x Code Frist For Oracle 实践与注意点

    Entity Framework 6.x Code Frist For Oracle 实践与注意点 开发环境 Visual Studio.net 2015/2017 Oracle 11g/12c 数据 ...

  5. Linux录屏软件

    如何查找录屏软件 apt-cache search screen record libutempter-dev - privileged helper for utmp/wtmp updates (d ...

  6. 微信小程序实例-获取当前的地理位置、速度

    微信小程序官方文档 https://mp.weixin.qq.com/debug/wxadoc/dev/api/location.html JS代码 //index.js //获取应用实例 var a ...

  7. C# WinForm 技巧十: 开发工具

    一.摘要   为了开发效率就应该为这个框架开发一个配套工具.来生成固定格式的代码.工具界面如下:   二.数据库整理篇   添加表主键 修改表说明 修改表字段说明 生成数据库文档 导出数据库里相同的字 ...

  8. C#按需序列化对象为Json字符串

    只贴代码,不解释了.新的代理类型确实很给力! public static class JsonHelper { public static string ToJsonString<T>(I ...

  9. ffrpc相关文章列表

    ffrpc 是异步c++通信库.可以说是传统rpc模式和zeromq模式的一个结合,采用broker模式封装client和server之间的拓扑关系,而client和server的通信仍然按照请求应答 ...

  10. mac或linux下xampp的mysql配置

    在mac 下安装好xampp后,需要在终端命令行操作时,比如输入:mysql -u root -p,未正确配置前不会出现想要的输入密码提示,而是会提示: command not found 原来当你输 ...