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. cmd for 循环拷贝文件

    这几天忙活部署测试环境, 中途需要拷贝 文件, 直接贴code吧: ::/定义原路径 set source=seventrat_test_backend,seventrat_test_frontend ...

  2. Nodejs npm安装socket.io报错解决办法

    安装socket.io时,报错,提示需要安装Microsoft visual studio 2005 或 Net framework 2.0 sdk,没有找到vcbuild.exe,解决办法是安装 . ...

  3. Crypto++入门学习笔记(DES、AES、RSA、SHA-256)(加解密)

    转自http://www.cppblog.com/ArthasLee/archive/2010/12/01/135186.html 最近,基于某些原因和需要,笔者需要去了解一下Crypto++库,然后 ...

  4. quick -- 创建精灵和动作

    local imgBg = display.newSprite("666666.jpg") :pos(display.cx, display.cy) :addTo(self) , ...

  5. BZOJ 4337: BJOI2015 树的同构 树hash

    4337: BJOI2015 树的同构 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4337 Description 树是一种很常见的数 ...

  6. Step by step Process of creating APD

    Step by step Process of creating APD: Business Scenario: Here we are going to create an APD on top o ...

  7. Akismet API 密钥(key)免费获取方法

    Akismet插件是用户使用最广泛的垃圾评论插件,也是wordpress的创始人制作的,同时它也毫无疑问的成为wordpress的默认安装插件,这样的插件可以帮助用户解决垃圾评论的烦恼,而且也不用访客 ...

  8. R语言之词云:wordcloud&wordcloud2安装及参数说明

    一.wordcloud安装说明 install.packages("wordcloud"); 二.wordcloud2安装说明 install.packages("dev ...

  9. WCF实例上下文模式与并发模式对性能的影响

    实例上下文模式 InstanceContextMode 控制在响应客户端调用时,如何分配服务实例.InstanceContextMode 可以设置为以下值: •Single – 为所有客户端调用分配一 ...

  10. CREATE A LOADING SCENE / SPLASH SCREEN - UNITY

    In the first scene or maybe the Main Menu scene of your game Create an Empty Gameobject. Call it wha ...