[抄题]:

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

[思维问题]:

[一句话思路]:

左边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. ORA-03113:通信通道的文件结尾

    问题: 用命令startup启动实例时,报错“ORA-03113:通信通道的文件结尾”. 解决: SQL> startup mount ORACLE 例程已经启动. Total System G ...

  2. golang web框架 beego 学习 (四) 连接mysql

    1 DB参数配置在app.conf appname = gowebProject httpport = runmode = dev [db] host= localhost port= databas ...

  3. move操作

    move一个表到另外一个表空间时,索引不会跟着一起move,而且会失效.(LOB类型例外) 表move,我们分为: *普通表move *分区表move *LONG,LOB大字段类型move来进行测试和 ...

  4. 使用for...of 优点,代替for...in,forEach和for循环

    来自阮一峰ES6标准: http://es6.ruanyifeng.com/#docs/iterator

  5. python(十二)下:ORM框架SQLAlchemy使用学习

    此出处:http://blog.csdn.net/fgf00/article/details/52949973 本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 ...

  6. PHP图片压缩

    <?php /** * 分享请保持网址.尊重别人劳动成果.谢谢. * 图片压缩类:通过缩放来压缩.如果要保持源图比例,把参数$percent保持为1即可. * 即使原比例压缩,也可大幅度缩小.数 ...

  7. 【C语言】09条件编译

    条件编译的概念;通常我们希望程序的其中一部分代码只有在满足一定的情况下才进行编译,否则不参与编译,(只有参与编译的代码最终才能被执行) 这就是条件编译; 基本用法; #if condication01 ...

  8. PHP过滤各种HTML标签的表达式,值得收藏

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  9. ajax的一些小知识

    eval()可以把一个字符串转化为本地的js代码来执行 <script type="text/javascript"> var str = "alert('h ...

  10. EOF与子过程返回

        在2000及其以上系统,P处理语句GOTO新增了:EOF系统标签,意思是移动到当前P处理文件的结尾,EOF==END OF FILE的缩写,意为文件结尾,主要表现形式为:GOTO :EOFOR ...