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. UVA 11090 - Going in Cycle!!(Bellman-Ford)

    UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...

  2. SVNKIT的low api应用之修改库中文件内容(File modification)

    最近在做一个仓库管理系统,架构在svn之上.要求每一项操作要记录在log文件中,弄了很久起初感觉无法向库中的文本文件添加东西,就是修改库中的文本文件.于是采用了一个很笨的办法:    现将库中的log ...

  3. 一起来开发Android的天气软件(四)——使用Gson解析数据

    离上一篇文章过去才4.5天,我们赶紧趁热打铁继续完毕该系列的天气软件的开发. 承接上一章的内容使用Volley实现网络的通信.返回给我们的是这一串Json数据{"weatherinfo&qu ...

  4. Knockout应用开发指南 第六章:加载或保存JSON数据

    原文:Knockout应用开发指南 第六章:加载或保存JSON数据 加载或保存JSON数据 Knockout可以实现很复杂的客户端交互,但是几乎所有的web应用程序都要和服务器端交换数据(至少为了本地 ...

  5. android maven eclipse里面新建mavenprojectThe desired archetype does not exist

    这个问题头疼死我了 又一次配置下你看我的教程 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmFpbmkxMTk=/font/5a6L5L2T/f ...

  6. 【PLSQL】package包的使用

    ************************************************************************   ****原文:blog.csdn.net/clar ...

  7. Oracle语句集锦

    创建用户并赋予dba权限 1)进入cmd 2)sqlplus / as sysdba 或者 sqlplus sys/密码 as sysdba SQL> conn sys/wcq123@orcl ...

  8. loading加载中效果

    (function(){ try{ var ui={ loading:{ addCssStyle:function(text) { var head = document.getElementsByT ...

  9. 在iOS7中改动状态栏字体的颜色

    状态栏的字体为黑色:UIStatusBarStyleDefault 状态栏的字体为白色:UIStatusBarStyleLightContent 一.在info.plist中,将View contro ...

  10. yum 简介及使用 安装、删除

    使用yum装软件很方便,这里简单介绍一下. Yum简介 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE.CentOS中的She ...