lintcode 1: Data Stream Median
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?
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]
.
Total run time in O(nlogn).
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的更多相关文章
- [OJ] Data Stream Median (Hard)
LintCode 81. Data Stream Median (Hard) 思路: 用一个大根堆保存较小的一半数, 一个小根堆保存较大的一半数. 每次根据num和两个堆顶的数据决定往哪个堆里面放. ...
- LintCode "Sliding Window Median" & "Data Stream Median"
Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...
- 数据流中位数 · data stream median
[抄题]: 数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数. [思维问题]: [一句话思路]: 左边x个元素,右边要有x+1个元素,因此利用maxheap把左边的最大值 ...
- [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 ...
- [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 ...
- Find Median from Data Stream
常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...
- 数据结构与算法(1)支线任务8——Find Median from Data Stream
题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...
- 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 ...
- 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 ...
随机推荐
- 10招让你成为杰出的Java程序员(转)
如果你是一个热衷于技术的 Java 程序员, 那么下面的 10 个要点可以让你在众多 Java 开发人员中脱颖而出. 1. 拥有扎实的基础和深刻理解 OO 原则 对于 Java 程序员,深刻理解 Ob ...
- 使用COM提供SafeArray数据
在前一篇博文<读取SafeArray数据>我们介绍了C#读取安全数组.那么我们的COM怎么编写呢? 1. 定义SAFEARRAY变量 SAFEARRAY *pSArray = NULL; ...
- class 添加样式,删,开关 【选择】addClass,removeClass,toggleClass
<1> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>< ...
- CentOS 6.5 配置 SSDB 1.8.0
环境说明: OS:CentOS 6.5 (阿里云ECS) 相关链接: 1.SSDB 下载配置:http://ssdb.io/docs/install.html 2.SSDB 入门文档:http:// ...
- Winform通用模块之流水号生成
打算接下来的时间里把自己觉得用起来还比较好用的通用模块,在这里向大家介绍一下,如果你有更好的想法时,也希望你不吝指点. 1.数据库表及存储过程 在介绍这个通用流水号生成的模块前,我们先来看一下其相关的 ...
- 登录oracle时,scott is locked (帐户被锁定) 的解决方法
登录Oracle时,用scott/tiger 通常此时会报一个错误: scott is locked (帐户被锁定) 现在就要用超级用户system将scott帐户进行解锁. cmd->sql ...
- HTTP请求响应过程 与HTTPS区别
原文:HTTP请求响应过程 与HTTPS区别 HTTP协议学习笔记,基础,干货 HTTP协议 HTTP协议主要应用是在服务器和客户端之间,客户端接受超文本. 服务器按照一定规则,发送到客户端(一般是浏 ...
- Hibernate主键生成策略简单总结
数据库表主键的知识点: Generator 为每个 POJO 的实例提供唯一标识. 一般情况,我们使用"native".class 表示采用由生成器接口net.sf.hiberna ...
- Ubuntu12.04编译Android4.0.1源码全过程-----附wubi安装ubuntu编译android源码硬盘空间不够的问题解决
昨晚在编译源码,make一段时间之后报错如下: # A fatal error has been detected by the Java Runtime Environment: # # SIGSE ...
- Swift正在使用NSURLConnection异步下载同步(实例解析)
原版的blog.转载请注明出处 http://blog.csdn.net/hello_hwc 一.同步异步两个概念 简单来讲.同步就是函数或者闭包(objective c中的block)运行完成才干返 ...