Data Stream Median

Numbers keep coming, return the median of numbers at every time a new number added.

Have you met this question in a real interview?

Example

For numbers coming list: [1, 2, 3, 4, 5], return [1, 1, 2, 2, 3].

For numbers coming list: [4, 5, 1, 3, 2, 6, 0], return [4, 4, 4, 3, 3, 3, 3].

For numbers coming list: [2, 20, 100], return [2, 2, 20].

Challenge

Total run time in O(nlogn).

Clarification

What's the definition of Median?

- Median is the number that in the middle of a sorted array. If there are n numbers in a sorted array A, the median isA[(n - 1) / 2]. For example, if
A=[1,2,3], median is 2. If A=[1,19], median is
1.

[思路]

用两个堆, max heap 和 min heap. 维持两个堆的大小相等(max堆能够比min堆多一个).  则max堆的顶即为median值.

[CODE]

public class Solution {
/**
* @param nums: A list of integers.
* @return: the median of numbers
*/
public int[] medianII(int[] nums) {
// write your code here
if(nums==null) return null;
int[] res = new int[nums.length]; PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(11, new Comparator<Integer>() {
@Override
public int compare(Integer x, Integer y) {
return y-x;
}
});
res[0] = nums[0];
maxHeap.add(nums[0]); for(int i=1; i<nums.length; i++) {
int x = maxHeap.peek();
if(nums[i] <= x) {
maxHeap.add(nums[i]);
} else {
minHeap.add(nums[i]);
}
if(maxHeap.size() > minHeap.size()+1 ) {
minHeap.add(maxHeap.poll());
} else if(maxHeap.size() < minHeap.size()) {
maxHeap.add(minHeap.poll());
}
res[i] = maxHeap.peek();
}
return res;
}
}

lintcode 1: Data Stream Median的更多相关文章

  1. [OJ] Data Stream Median (Hard)

    LintCode 81. Data Stream Median (Hard) 思路: 用一个大根堆保存较小的一半数, 一个小根堆保存较大的一半数. 每次根据num和两个堆顶的数据决定往哪个堆里面放. ...

  2. LintCode "Sliding Window Median" & "Data Stream Median"

    Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...

  3. 数据流中位数 · data stream median

    [抄题]: 数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数. [思维问题]: [一句话思路]: 左边x个元素,右边要有x+1个元素,因此利用maxheap把左边的最大值 ...

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

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

  6. Find Median from Data Stream

    常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...

  7. 数据结构与算法(1)支线任务8——Find Median from Data Stream

    题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...

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

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

随机推荐

  1. C# Dictionary.Add(key,value) 与 Dictionary[key]=value的区别

    1. MSDN上的描述. http://msdn.microsoft.com/zh-cn/library/9tee9ht2(v=VS.85).aspx 通过设置 Dictionary 中不存在的键值, ...

  2. document.write()相关

    原文地址:http://www.cnblogs.com/dh616854836/articles/2140349.html document.write()脚本向窗口(不管是本窗口或其他窗口)写完内容 ...

  3. Urban Dictionary: psd

    Urban Dictionary: psd psd Share on twitter Share on facebook Share on more 3 up, 1 down It means Poo ...

  4. yum 安装软件时报错

    报错信息 Another app is currently holding the yum lock; waiting for it to exit 处理方法 rm -rf /var/run/yum. ...

  5. SVNKIT的SVNCommitClient的doMkDir的操作

    package com.repositoryclient.svnoptions; import java.io.File; import org.tmatesoft.svn.core.SVNCommi ...

  6. 项目中那些事|ListView中嵌套ListView问题

    要在一个ListView中放入另一个ListView,也即在一个ListView的每个 item 中放入另外一个ListView.但刚开始的时候,会发现放入的子ListView会显示不完全(我这里只显 ...

  7. WebSocket API

    WebSocket API 这一章介绍如何用WebSocket API来控制协议和创建应用,运用http://websocket.org 提供的现有WebSocket服务器,我们可以收发消息.创建一些 ...

  8. HashMap-死锁导致cpu占用100%分析(转)

    最近项目里面的一段千年代码出了问题,这个问题以前也出现过,不过不是那么明显,这次迁移机器由以前的4台机子变成2台以后问题被放大,最终不得不解决,特此分析一下. 先放出问题的代码 ? 1 2 3 4 5 ...

  9. QTP脚本--应用参数化来测试某个输入框

    以前一直觉得自己没有写代码的资质,太急于求成,以为一天就能写好几个功能,几千行代码,于是就没耐心了,没心情学下去了....但是最近发现其实写代码是一个漫长的过程,都是在修修改改中成长起来的.于是今天试 ...

  10. Yii/Yii2:查询返回以某特定列为索引(键)的数组

    在Yii1.x中,要想查询语句返回以特定列为键(key)的数组数据,代码例如以下(下述样例以表主键为返回数组索引): $users = User::model()->findAll(array( ...