Data Stream Median

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

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 is A[(n - 1) / 2]. For example, ifA=[1,2,3], median is 2. If A=[1,19], median is 1.

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

分析:
使用两个heap,一个min heap(root的值最小) and max heap (root的值最大)。保持两个heap的size差别最大为1,并且,min heap永远不会不max heap的size大。

 1 public class Solution {
 2     /**
 3      * @param nums: A list of integers.
 4      * @return: the median of numbers
 5      */
 6     public int[] medianII(int[] nums) {
 7         if (nums == null || nums.length == 0)
 8             return null;
 9
10         int[] med = new int[nums.length];
11
12         PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
13         PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(11, new Comparator<Integer>() {
14             public int compare(Integer x, Integer y) {
15                 return y - x;
16             }
17         });
18
19         med[0] = nums[0];
20         maxHeap.offer(nums[0]);
21
22         for (int i = 1; i < nums.length; i++) {
23             if (nums[i] < maxHeap.peek()) {
24                 maxHeap.offer(nums[i]);
25             } else {
26                 minHeap.offer(nums[i]);
27             }
28
29             if (maxHeap.size() > minHeap.size() + 1) {
30                 minHeap.offer(maxHeap.poll());
31             } else if (maxHeap.size() < minHeap.size()) {
32                 maxHeap.offer(minHeap.poll());
33             }
34             med[i] = maxHeap.peek();
35         }
36         return med;
37     }
38 }
时间: 2024-10-08 10:34:05

Data Stream Median的相关文章

【Lintcode】LRU Cache, Data Stream Median

主要是priority_queue的用法 一个是内置类型优先队列怎么设置小根堆(默认大根堆) 如果是自定义数据结构,有两种办法 1.定义这种数据结构的比较符号,就可以当成内置类型整 2.传进去一个重载()的类,当小于号用,默认还是大根堆,也许传进去的是个callable object都行的吧,我试了一下函数好像不行,不懂,不管了 LRU Cache class LRUCache{ public: // @param capacity, an integer int Time; typedef i

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? Example For numbers coming list: [1, 2, 3, 4, 5], return [1, 1, 2, 2, 3]. For numbers coming list:

数据流中位数 &#183; data stream median

[抄题]: 数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数. [思维问题]: [一句话思路]: 如果maxHeap.peek() > minHeap.peek(),就不断流动,直到顺滑. [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: 接口类是Queue<Integer>,指明里面的数据类型 compare类无参数,里面的方法有参数 maxheap也有参数,是cnt,cpr,因为要用

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

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 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 De

支线任务8-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 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 Des

LeetCode之Find Median from Data Stream

1.题目描述 翻译过来就是对输入的数字找出其中位数. 2.解题思路 一开始,我想着这是动态输入,所以必须放弃数组这些静态的数据结构.首先想到了平衡二叉树,然而太麻烦了.后面又想到了大顶堆以及小顶堆,但是不知如何运用,就上这道题discuss瞅了瞅.结果发现了一种double heap解法,十分带感,总体思路就是比中位数大的数字放一边(大数堆),小的放另一边(小数堆).然后也接触了并且运用一种新的队列用法——优先队列.插入数字过程中,始终保持大数堆大小大于等于小数堆,以便最后输出中位数.代码如下:

Find Median from Data Stream

常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector<int>& coll,int idx,int max){ int left=2*idx+1,right=2*idx+2; int largest=idx; if(left<max&&coll[left]>coll[idx]) largest=left; if(rig

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

题目如下:(https://leetcode.com/problems/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 middle value. So the median is the mean of the two middle value. Examples: [2,3,4]