Find Median from Data Stream 解答
Question
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
Design a data structure that supports the following two operations:
- void addNum(int num) - Add a integer number from the data stream to the data structure.
- double findMedian() - Return the median of all elements so far.
For example:
add(1)
add(2)
findMedian() -> 1.5
add(3)
findMedian() -> 2
Solution 1 -- Heap
我们可以依据这个思路来想这道题:
median -> 中位数 -> 中位数两边的subarrays的大小差不超过1 -> 什么数据结构可以满足实时地将输入数组排序,并且保持一半一半的大小?
第一选择是Heap
我们维护一个MaxHeap用来存前半段的小的数据,维护一个MinHeap用来存后半段的大的数据
每次加入一个新数字,我们进行如下顺序判断:
1. MaxHeap是否是空,如果是,加入MaxHeap
2. num是否小于MaxHeap的最大值,如果是,加入MaxHeap
3. MinHeap是否是空,如果是,加入MinHeap
4. MaxHeap和MinHeap都不空
num 和 MaxHeap的最大值 和 MinHeap的最小值 比较
class MedianFinder {
private PriorityQueue<Integer> minHeap;
private PriorityQueue<Integer> maxHeap;
private Double median; public MedianFinder() {
minHeap = new PriorityQueue<Integer>(11);
maxHeap = new PriorityQueue<Integer>(11, Collections.reverseOrder());
median = null;
} // Adds a number into the data structure.
public void addNum(int num) {
int size1 = maxHeap.size();
int size2 = minHeap.size();
if (size1 == 0) {
maxHeap.add(num);
} else if (num <= maxHeap.peek()) {
maxHeap.add(num);
} else if (size2 == 0) {
minHeap.add(num);
}else {
int firstMax = maxHeap.peek();
int secondMin = minHeap.peek();
if (num <= firstMax) {
maxHeap.add(num);
} else if (num >= secondMin) {
minHeap.add(num);
} else{
maxHeap.add(num);
}
}
// Check balance
size1 = maxHeap.size();
size2 = minHeap.size();
if (size1 > size2 + 1) {
while (size1 > size2 + 1) {
int top = maxHeap.poll();
minHeap.offer(top);
size1--;
size2++;
}
} else if (size2 > size1 + 1) {
while (size2 > size1 + 1) {
int top = minHeap.poll();
maxHeap.offer(top);
size2--;
size1++;
}
}
if (size2 == size1 + 1)
median = (double) minHeap.peek();
if (size1 == size2 + 1)
median = (double) maxHeap.peek();
if (size1 == size2 && size1 > 0)
median = ((double) maxHeap.peek() + (double) minHeap.peek()) / 2;
} // Returns the median of current data stream
public double findMedian() {
return median.doubleValue();
}
}; // Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf = new MedianFinder();
// mf.addNum(1);
// mf.findMedian();
Solution 2 -- Balanced BST
平衡二叉树也可以满足条件,但是实际代码太多。所以可以在面试时优先用heap写出代码,然后follow-up的时候提出平衡二叉树的思想。
Find Median from Data Stream 解答的更多相关文章
- [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] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- [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 ...
- leetcode@ [295]Find Median from Data Stream
https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...
随机推荐
- <php>PDO用法一
<?php //造PDO对象 $pdo = new PDO("mysql:dbname=mydb;host=localhost","root"," ...
- shell输出加颜色
shell输出加颜色 #cat a.sh #!/bin/sh blue=`tput setaf 4` reset=`tput sgr0` echo "${blue}[INFORMATION] ...
- nmap 使用脚本引擎进行扫描
1.下载nmap(nmap官网). 2.安装nmap. 3.编辑环境变量(windows下所需),保存.
- Asp.Net HttpApplication请求管道与Session(一)
1.请求处理顺序执行事件 /********************请求处理顺序执行事件**********************/ /// <summary> /// 请求入站 /// ...
- 解决Android AVD启动报错问题
好不容易从ADT Bundle转为Android Studio的开发环境,一路荆棘,现在又遇到了模拟器的问题,本来直接用真机调试程序会更快些,但是为了模拟多种系统不得不开启AVD. 废话不说,问题和解 ...
- (转)IIS7 下部署Asp.net应用
最近在部署一个ASP.NET的应用到IIS7中的时候,遇到了一些问题,现在把部署中的遇到的问题和部署步骤进行总结一下,本文中只涉及到ASP.NET的基本部署. 一. 部署环境 Windows 7 ...
- vi 快捷键【转】【weber整理必出精品】
光标的移动 命令 光标移动 h或^h 向左移一个字符 j或^j或^n 向下移一行 k或^p 向上移一行 l或空格 向右移一个字符 G 移到文件的最后一行 nG 移到文件的第n行 w 移到下一个字的开头 ...
- 关于sql2005 与 myeclipse进行连接出现的小问题
C盘目录下没有jdbc这个文件夹,所以从网上下一个 这个是2008连接jdbc用的 正常解压第一个到相应的目录 主要是注意一个叫tcp/ip的协议,米有找到64位的 点击这里的tcp ip就哦了但是他 ...
- Sql 函数大全 (更新中...由难到简
1.字符处理类: 1.1 指定指定字符输出的次数 ) 结果:1a1a1a1a1a (5个1a)
- Floyd最小环
本文转自这里 最小环:从一个点出发,经过一条简单路径回到起点成为环.图的最小环就是所有环中长度最小的. 怎样求最小环呢? 1传统的解决方法(dijkstra): 任意一个最小环环的权值, ...