数据流中位数 · data stream median
[抄题]:
数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数。
[思维问题]:
[一句话思路]:
左边x个元素,右边要有x+1个元素,因此利用maxheap把左边的最大值揪出来,利用minheap把右边的最小值揪出来
如果maxHeap.peek() > minHeap.peek(),就不断流动,直到顺滑。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:


[一刷]:
- 接口类是Queue<Integer>,指明里面的数据类型
- compare类无参数,里面的方法有参数
- maxheap也有参数,是cnt,cpr,因为要用到比较
- 这道题要求的是 不断添加之后,返回一个ans[]
[二刷]:
- 如果minHeap.isEmpty(),才需要讲总元素个数加一
- MinHeap,MaxHeap,numOfElements都是几个函数公用的数据结构,要声明为private类型后放在外面
[三刷]:
[四刷]:
[五刷]:
[总结]:
[复杂度]:Time complexity: O(n个数*add的lgn) Space complexity: O(n)
[英文数据结构,为什么不用别的数据结构]:
[其他解法]:
[Follow Up]:
[题目变变变]:
public class Solution {
/*
* @param nums: A list of integers
* @return: the median of numbers
*/
private Queue<Integer> MinHeap,MaxHeap;
private int numOfElements = 0;
public int[] medianII(int[] nums) {
int cnt = nums.length;
Comparator<Integer> revcmp = new Comparator<Integer>() {
public int compare(Integer left,Integer right) {
return right.compareTo(left);
}
};
MinHeap = new PriorityQueue<Integer>(cnt);
MaxHeap = new PriorityQueue<Integer>(cnt,revcmp);
int[] ans = new int[cnt];
for (int i = 0; i < cnt; i++) {
addNumber(nums[i]);
ans[i] = getMedian();
}
return ans;
}
//addNumber
private void addNumber(int value) {
MaxHeap.add(value);
if (numOfElements % 2 == 0) {
if (MinHeap.isEmpty()) {
numOfElements = 1;
return ;
}
else if (MaxHeap.peek() > MinHeap.peek()) {
int root_Of_MaxHeap = MaxHeap.poll();
int root_Of_MinHeap = MinHeap.poll();
MaxHeap.add(root_Of_MinHeap);
MinHeap.add(root_Of_MaxHeap);
}
}
else {
MinHeap.add(MaxHeap.poll());
}
numOfElements++;
}
//getMedian
private int getMedian() {
return MaxHeap.peek();
}
}
数据流中位数 · data stream median的更多相关文章
- [OJ] Data Stream Median (Hard)
LintCode 81. Data Stream Median (Hard) 思路: 用一个大根堆保存较小的一半数, 一个小根堆保存较大的一半数. 每次根据num和两个堆顶的数据决定往哪个堆里面放. ...
- lintcode 1: Data Stream Median
Data Stream Median Numbers keep coming, return the median of numbers at every time a new number adde ...
- LintCode "Sliding Window Median" & "Data Stream Median"
Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...
- [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 ...
- [Swift]LeetCode295. 数据流的中位数 | 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 ...
- [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...
- [LeetCode] 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 no ...
- [leetcode]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 no ...
随机推荐
- python3 tkinter
https://morvanzhou.github.io/tutorials/python-basic/tkinter/ 李导师推荐的.非常感谢.非常棒的视频教程!!! 只不过里面的教程视频是yout ...
- [UE4]蓝图函数库小结
蓝图函数库的功能非常强劲,如果在项目中使用的话有时能达到事半功倍的效果. 蓝图函数库,Blueprint Function Library.可以非常方便的将代码中的函数暴露给所有的蓝图使用,同时也提供 ...
- centos 7 mount usb hard disk(ntfs format)
1. yum install -y epel-release* 2. yum install -y ntfs-3g 3. 命令:fdisk -l (查看磁盘分区信息) [root@devserverg ...
- 使用nproxy代理本地服务到内网
前端开发中:很多场景需要在局域网下的其他手机或设备查看网页, 问题来了, web服务部署在本机的某个端口上(8080),只能通过本机浏览器访问, 怎样能让局域网下的其他设备也访问呢?可能你会说 关闭 ...
- 2018-2019-2 《网络对抗技术》Exp1 PC平台逆向破解 Week3 20165233
Exp1 PC平台逆向破解 实验内容 一.基础知识点 NOP, JNE, JE, JMP, CMP汇编指令的机器码 NOP指令即"空指令",执行到NOP指令时,CPU什么也不做,机 ...
- python入门-函数(一)
1定义函数并且调用 注释语句""" """ def greet_user(): """显示简单的问候语&qu ...
- jquery ajax 赋值问题, 后面程序判断逻辑用
添加async:false; 像 $.get() 等是没有async参数的, 所以需要用到如下 $.ajaxSetup({ async : false //这个意思是等这个ajax有了返回值后才会执行 ...
- 序列化和反序列化(json 和pickle)dumps 为序列化, json为反序列化
json 可以在不同语言中进行使用 下面先介绍一下json的适用方法 import json, pickle t1 = { 'name':'alex', ', ' } t1 = json.dumps( ...
- JS 时间 获取 当天,昨日,本周,上周,本月,上月
调用 setTimeRange (2); function setTimeRange (v) { var fmt = 'YYYY-MM-DD HH:mm'; var now = new Date() ...
- WP8.1 控件默认字体颜色 配置文件位置
C:\Program Files (x86)\Windows Phone Kits\8.1\Include\abi\Xaml\Design\generic.xaml 可在App.xaml文件中over ...