[抄题]:

数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数。

[思维问题]:

[一句话思路]:

左边x个元素,右边要有x+1个元素,因此利用maxheap把左边的最大值揪出来,利用minheap把右边的最小值揪出来

如果maxHeap.peek() > minHeap.peek(),就不断流动,直到顺滑。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 接口类是Queue<Integer>,指明里面的数据类型
  2. compare类无参数,里面的方法有参数
  3. maxheap也有参数,是cnt,cpr,因为要用到比较
  4. 这道题要求的是 不断添加之后,返回一个ans[]

[二刷]:

  1. 如果minHeap.isEmpty(),才需要讲总元素个数加一
  2. 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的更多相关文章

  1. [OJ] Data Stream Median (Hard)

    LintCode 81. Data Stream Median (Hard) 思路: 用一个大根堆保存较小的一半数, 一个小根堆保存较大的一半数. 每次根据num和两个堆顶的数据决定往哪个堆里面放. ...

  2. lintcode 1: Data Stream Median

    Data Stream Median Numbers keep coming, return the median of numbers at every time a new number adde ...

  3. LintCode "Sliding Window Median" & "Data Stream Median"

    Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...

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

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

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

  7. [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)

    295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...

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

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

随机推荐

  1. Linux期中架构 全网备份案例

    server端脚本 #!/bin/bash #1 进行数据完整性验证 并生成结果 find /backup -type f -name "finger.txt"| xargs md ...

  2. SpringBoot学习记(一)第一个SpringBoot Web服务

    工具IDEA 一.构建项目 1.选择Spring Initializr 2.填写项目信息 3.勾选webService 4.勾选Thymeleaf 5.项目建立完成,启动类自动生成 二.写个Contr ...

  3. sklearn-MultinomialNB朴素贝叶斯分类器

    原型 class sklearn.naive_bayes.MultinomialNB(alpha=1.0, fit_prior=True, class_prior=None) 参数 Parameter ...

  4. CSS border边框属性教程(color style)

    CSS 边框即CSS border-border边框样式颜色.边框样式.边框宽度的语法结构与应用案例教程篇 一.CSS 边框基础知识 CSS 边框即CSS border是控制对象的边框边线宽度.颜色. ...

  5. TensorFlow系列专题(一):机器学习基础

  6. 中国标准时间改为formatTime格式

    1.toLocaleDateString (根据本地时间把Date 对象的日期部分转换为字符串): var time = new Date(); var formatTime = time.toLoc ...

  7. spring 注解 @NotBlank and BingResult

    @NotEmpty用在集合类上面 @NotBlank 用在String上面 @NotNull 用在基本类型上 在 user对象中需要

  8. Redis 通用操作2

    01, 一次设置多个键值 => mset key1 value1 key2 value2 key3 value3 ...... 02, 一次获取多个值 => mget ke1 key2 k ...

  9. delphi RAD XE 安装路径 重装备份

    重装的时候,不要删除c盘C:\ProgramData下的guid目录.以便完整卸载旧版本. 控件安装的生成的目标文件路径 C:\Users\Public\Documents\Embarcadero\S ...

  10. Window Mysql 5.7.18安装

    1:下载地址 https://dev.mysql.com/downloads/mysql/ 点击Community , 然后点击Community Server 位置, 下载 安装包 2: 配置环境变 ...