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 ...
随机推荐
- Codeforces Round #272 (Div. 1) Problem C. Dreamoon and Strings
C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...
- c语言二维数组变色龙之死字的打印
1 #include <stdio.h> #include <stdlib.h> void main() { ][]= { {'#','#','#',' ','#','#',' ...
- 【转】TI蓝牙BLE 协议栈代码学习
BLE就是低功率蓝牙.要着重了解两种设备: dual-mode双模设备:简单说就是向下兼容. single-mode单模设备:仅仅支持BLE. 关于开发主要讲的是单模设备,它可以只靠纽扣电池即可持 ...
- mysql 获取当前时间戳
mysql 获取当前时间为select now() 运行结果: 2012-09-05 17:24:15 mysql 获取当前时间戳为select unix_timestamp(now()) 运行结 ...
- python下载多个文件
# -*- coding: utf-8 -*-__author__ = 'Administrator'import urllib2,urllib,os,redef Url1(url):#多个文件 ...
- LR翻页脚本并在每页实现业务操作
性能需求:在列表中删除后有记录,或对列表中的每条记录进行操作(如点击每条记录的“单号”进入订单详情页面,或在列表中对每条记录进行“启用”.“停止”操作) 举例:Vuser脚本模拟用户在订单列表中点击每 ...
- easyUI的datagrid控件日期列不能正确显示Json格式数据的解决方案
EasyUI是一套比较轻巧易用的Jquery控件,在使用过程中遇到一个问题,它的列表控件——datagrid, 在显示日期列的时候,由于后台返回给页面的数据是Json格式的,其中的日期字段,在后台是正 ...
- Jboss基础及简单的应用
初学Jboss,对于Jboss的基础认识以及配置做一些记录 Jboss基础: JBoss是什么–基于J2EE的应用服务器–开放源代码–JBoss核心服务不包括支持servlet/JSP的WEB容器,一 ...
- HDU 5904 - LCIS (BestCoder Round #87)
HDU 5904 - LCIS [ DP ] BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...
- 把python项目部署到centos里
.安装centos VMware9下面安装centos .在centos下面设置共享文件夹为你本地的论坛的代码,然后设置网络为桥接:直接连接到物理网络,赋值网络连接状态 .进入forum_svr.py ...