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 ...
随机推荐
- Vagrant - 百度百科
http://wapbaike.baidu.com/view/9201587.htm?ssid=0&from=844b&uid=3151E6C0905477A13653132D762B ...
- Android 从相冊获取近期拍摄的多张照片(获取相机拍照所存储的照片)
转载请标明出处:http://blog.csdn.net/android_ls/article/details/39928519 在做公司项目时.遇到的需求:自己定义显示照片的网格视图,显示用户近期採 ...
- PHP监測memcache服务端的执行状况
. 代码例如以下,代码为memcache官方代码,引用在此.做一下简单的说明: 1.设置username和password define('ADMIN_USERNAME','admin'); // A ...
- WebKit爬虫
https://github.com/emyller/webkitcrawler 一个开源的项目,可以快速入门. http://spiderformysql.com/ http://crawl.gro ...
- windows之实现3D立体效果的三种方法
第一种:快捷键:win+tab 另外一种:cmd输入rundll32.exe dwmapi #105 第三种:使用软件bumptop
- SVN中正确的add操作和delete操作
add操作: delete操作:
- java枚举的使用
定义枚举 public enum YesOrNo { YES("是") , NO("否") ; private String text ; private in ...
- C语言sizeofkeyword
说明: ******C语言sizeof是keyword.是一个操作符.它不是一个函数.用于计算可变.或内存数据字节数占用类型. ******sizeof有三种不同的方式: ***sizeof(变量名) ...
- C#之再议数组和集合
1.数组 1.1简单数组 1.2 多维数组 1.3锯齿数组 1.4Array数组 1.5作为参数的数组 1.6枚举 1.7结构 以上部分可参考 http://www.cnblogs.com/ztb12 ...
- zend studio 10 实现代码自动换行
在一篇zend framework 的PHP编码标准的文章中看到了这么一段: 一行 80 字符以内是比较合适,就是说,ZF 的开发者应当努力在可能的情况下保持每行代码少于 80 个字符,在有些情况下, ...