当前位置:网站首页>Sword finger offer: the median in the data stream (priority queue large top heap small top heap leetcode 295)
Sword finger offer: the median in the data stream (priority queue large top heap small top heap leetcode 295)
2022-04-23 04:48:00 【wyplj_ sir】
subject :
How to get the median in a data stream ? If you read an odd number of values from the data stream , So the median is the number in the middle after all the numbers are sorted . If even numbers are read from the data stream , Then the median is the average of the middle two numbers after all the numbers are sorted . We use Insert() Method to read the data stream , Use GetMedian() Method to get the median of the currently read data .
answer :
Solution 1 :
Sort directly first , The code is as follows :
import java.util.*;
public class Solution {
ArrayList<Integer> list = new ArrayList<>();
public void Insert(Integer num) {
list.add(num);
}
public Double GetMedian() {
Collections.sort(list);
int size = list.size();
if(size==0){
return 0.00;
}
if(size==1){
return (double)list.get(0);
}
if(size%2!=0){
return (double)list.get(size/2);
}else{
return ((double)(list.get(size/2)+(double)list.get(size/2-1))/2.00);
}
}
}
Solution 2 :
The largest heap and the smallest heap , Ideas as follows (leetcode On someone else's picture , See the link below for the source ):
The code is as follows :
import java.util.*;
public class Solution {
PriorityQueue<Integer> maxheap = new PriorityQueue<>((a,b)->b-a);
PriorityQueue<Integer> minheap = new PriorityQueue<>();
int count = 0;
public void Insert(Integer num) {
if(count%2==0){
minheap.add(num);
maxheap.add(minheap.poll());
}else{
maxheap.add(num);
minheap.add(maxheap.poll());
}
count++;
}
public Double GetMedian() {
if(count%2==0){
// If it's even , Returns the average value of the top elements of the small top heap and the large top heap
return (double)(minheap.peek()+maxheap.peek())/2;
}else{
// If it's odd , Returns the top element of the big top heap
return (double)maxheap.peek();
}
}
}
Reference link :
版权声明
本文为[wyplj_ sir]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220555178756.html
边栏推荐
- [paper reading] [3D target detection] point transformer
- Graduation project
- Perfect test of coil in wireless charging system with LCR meter
- FAQ of foreign lead and alliance Manager
- Progress of innovation training (III)
- Code007 -- determine whether the string in parentheses matches
- Migrate from MySQL database to AWS dynamodb
- CLion+OpenCV identify ID number - detect ID number
- Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
- 拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
猜你喜欢
Wechat payment function
Programmers complain: I really can't live with a salary of 12000. Netizen: how can I say 3000
View analysis of scenic spots in ArcGIS
Repair of self calibration SPC failure of Tektronix oscilloscope dpo3054
【数据库】MySQL基本操作(基操~)
383. Ransom letter
Recommended scheme for national production of electronic components for wireless charging
Introduction to raspberry pie 3B - system installation
Mysql50 basic exercises
POI export message list (including pictures)
随机推荐
Com alibaba. Common methods of fastjson
QML advanced (IV) - drawing custom controls
Pixel mobile phone brick rescue tutorial
Recommended scheme for national production of electronic components of wireless keyboard
Progress of innovation training (III)
io. Platform. packageRoot; // ignore: deprecated_ Member_ use
leetcode004--罗马数字转整数
Spark small case - RDD, spark SQL
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
Recommended scheme of national manufactured electronic components
The unity camera rotates with the mouse
La caméra Unity tourne avec la souris
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
Innovation training (XI) airline ticket crawling company information
Innovation training (VII) FBV view & CBV view
New terminal play method: script guidance independent of technology stack
Programmers complain: I really can't live with a salary of 12000. Netizen: how can I say 3000
Implementation of switching windows and capturing data in selenium mode
leetcode007--判断字符串中的括号是否匹配
Unity3d practical skills - theoretical knowledge base (I)