Array - 295. Find Median from Data Stream
时间:2022-07-25
本文章向大家介绍Array - 295. Find Median from Data Stream,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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 middle value. So the median is the mean of the two middle value.
For example,
[2,3,4]
, the median is 3
[2,3]
, the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
- void addNum(int num) - Add a integer number from the data stream to the data structure.
- double findMedian() - Return the median of all elements so far.
Example:
addNum(1) addNum(2) findMedian() -> 1.5 addNum(3) findMedian() -> 2
Follow up:
- If all integer numbers from the stream are between 0 and 100, how would you optimize it?
- If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it?
思路:
这题是实现几个接口,保证接口可以找出数据流的中位数,bf肯定是可以做,但是更好的解法就是使用两个堆或者优先队列,只要找出两个堆的根元素,也就是小根堆的堆顶和大根堆的堆顶元素,两数之和的一半就是中位数。还可以使用bst来做,两个左右指针,来确定中位数。
代码:
java:
class MedianFinder {
PriorityQueue<Integer> min = null;
PriorityQueue<Integer> max = null;
/** initialize your data structure here. */
public MedianFinder() {
min = new PriorityQueue();
max = new PriorityQueue(10, Collections.reverseOrder());
}
public void addNum(int num) {
max.offer(num);
min.offer(max.poll());
if (max.size() < min.size()) max.offer(min.poll());
}
public double findMedian() {
if (max.size() == min.size()) return (max.peek() + min.peek()) / 2.0;
else return max.peek();
}
}
/*class MedianFinder {
ArrayList<Integer> nums;
public int findInsertIndex(int target) {
int low = 0;
int high = nums.size() - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (nums.get(mid) >= target)
high = mid - 1;
else
low = mid + 1;
}
return low;
}
public MedianFinder() {
nums = new ArrayList<>();
}
public void addNum(int num) {
if (nums.size() == 0) {
nums.add(num);
} else {
int index = findInsertIndex(num);
if (index == nums.size()) {
nums.add(num);
} else {
nums.add(index, num);
}
}
}
public double findMedian() {
int mid = (nums.size() - 1) / 2;
return nums.size() % 2 == 0 ? (nums.get(mid) + nums.get(mid + 1)) / 2.0 : nums.get(mid);
}
}*/
- R语言爬虫实战——知乎live课程数据爬取实战
- Python爬虫系列(二)Quotes to Scrape(谚语网站的爬取实战)
- R语言数据清洗实战——世界濒危遗产地数据爬取案例
- Leetcode-Easy 437. Path Sum III
- R语言爬虫实战——网易云课堂数据分析课程板块数据爬取
- 左手用R右手Python系列14——日期与时间处理
- 如何使用管道操作符优雅的书写R语言代码
- 第四周编程作业(二)-Deep Neural Network for Image Classification: ApplicationDeep Neural Network for Image Cl
- 第四周编程作业(一)-Building your Deep Neural Network: Step by StepBuilding your Deep Neural Network: Step by
- Python数据抓取与可视化实战——网易云课堂人工智能与大数据板块课程实战
- 商务图表案例——仿经济学人分组漏斗图~
- 第二周编程作业 -Logistic Regression with a Neural Network mindsetLogistic Regression with a Neural Network
- 左手用R右手Python系列——任务进度管理
- 左右用R右手Pyhon系列——趣直播课程抓取实战
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法