Data Stream Median
来源:互联网 发布:查询域名注册信息 编辑:程序博客网 时间:2024/05/17 12:19
Numbers keep coming, return the median of numbers at every time a new number added.
Clarification
What's the definition of Median?
- Median is the number that in the middle of a sorted array. If there are n numbers in a sorted array A, the median is A[(n - 1) / 2]
. For example, if A=[1,2,3]
, median is 2
. If A=[1,19]
, median is 1
.
Example
For numbers coming list: [1, 2, 3, 4, 5]
, return [1, 1, 2, 2, 3]
.
For numbers coming list: [4, 5, 1, 3, 2, 6, 0]
, return [4, 4, 4, 3, 3, 3, 3]
.
For numbers coming list: [2, 20, 100]
, return [2, 2, 20]
.
java
这个方法超时,但却是最为直接想到的一个方法
public class Solution { /* * @param nums: A list of integers * @return: the median of numbers */ public int[] medianII(int[] nums) { // write your code here Queue<Integer> queue = new PriorityQueue<>(); List<Integer> list = new ArrayList<>(); Queue<Integer> temp; int val = 0; int cache = 0; for (int i = 0; i < nums.length; i++) { queue.offer(nums[i]); val = queue.size(); temp = new PriorityQueue<Integer>(queue); int j = (val - 1) / 2; while(j > 0) { temp.poll(); j--; } list.add(temp.poll()); } int[] arr = new int[list.size()]; for (int i = 0; i < list.size(); i++) { arr[i] = list.get(i); } return arr; }}
阅读全文
0 0
- [刷题]Data Stream Median
- lintcode: Data Stream Median
- Data Stream Median
- Data Stream Median
- Find Median from Data Stream
- lintcode 1: Data Stream Median
- Find Median from Data Stream
- Find Median from Data Stream
- Find Median from Data Stream
- Find Median from Data Stream
- Find Median from Data Stream
- LintCode 81:Data Stream Median
- Find Median from Data Stream
- Find Median from Data Stream leetcode 295
- LeetCode Find Median from Data Stream
- leetcode 295:Find Median from Data Stream
- [leetcode295]Find Median from Data Stream
- leetcode 295 : Find Median from Data Stream
- 二分图最大匹配(匈牙利算法)整理
- JZOJ 5419. 【NOIP2017提高A组集训10.24】筹备计划
- MATLAB教程(1) MATLAB 基础知识
- 数组的特点,数组的使用,链表的特点,set和list的区别,arrayList,linkedList,的常用方法,set,hashset,
- bzoj2013
- Data Stream Median
- 47. Permutations II
- 解决Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder!
- 监控中心方案设计
- python __slot__
- P
- Spark SQL 个人总结
- C++进阶—>互斥量:Event控制:多线程实现生产者-消费者例子
- new string("xiaolixi")的参数问题