数据流中位数 · 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 ...
随机推荐
- [UE4]C++ STL总结
STL概述 STL (Standard Template Library, 标准模板库) 是惠普实验室开发的一系列软件的统称.主要核心分为三大部分:容器(container).算法(algorithm ...
- Spring bean注解配置(1)
Spring自带的@Component注解及扩展@Repository.@Service.@Controller,如图 在使用注解方式配置bean时,需要引进一个包: 使用方法: 1.为需要使用注解方 ...
- Hive基础之Hive的复杂类型
ARRAY 一组有序字段,字段的类型必须相同.Array(1,2) create table hive_array(ip string, uid array<string>) row fo ...
- Hive基础之Hive表常用操作
本案例使用的数据均来源于Oracle自带的emp和dept表 创建表 语法: CREATE [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name ...
- centos6性能监控软件
常用软件在此下载 http://rpm.pbone.net/ http://pkgs.org/ collectl 显示cpu\disk\network的实时信息http://dl.fedoraproj ...
- Node.js 介绍及安装
Node.js是一个Javascript运行环境(runtime environment),发布于2009年5月,由Ryan Dahl开发,实质是对Chrome V8引擎进行了封装.本文详细介绍了No ...
- ES6进一步整理
内容: 1.变量及赋值 2.函数 3.数组及json 4.字符串 5.面向对象 6.Promise 7.generator 8.模块 1.变量及赋值 (1)ES5变量定义 var: 可以重复定 ...
- 20180130之PYTHON学习笔记【PYTHON3写个自动听课功能】
-----------------------原始实现想法------------ import pyautoguifrom PIL import Image#img=Image.open('c:/p ...
- Xeon Phi 编程备忘
▶ 闲鱼的 Xeon Phi 3120A 配办公室的新 Xeon 服务器,记录一下环境安装过程. ● 原本尝试搭 Ubuntu 服务器,参考[https://software.intel.com/en ...
- spring boot 整合 (全)
参考: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples