当前位置:网站首页>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
边栏推荐
- 520. Detect capital letters
- New terminal play method: script guidance independent of technology stack
- Basic operation of sequence table
- Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
- leetcode007--判断字符串中的括号是否匹配
- The object needs to add additional attributes. There is no need to add attributes in the entity. The required information is returned
- Record the blind injection script
- Innovative practice of short video content understanding and generation technology in meituan
- redis和mysql区别
- L2-011 玩转二叉树(建树+BFS)
猜你喜欢

229. Find mode II

Painless upgrade of pixel series

Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘

Opencv + clion face recognition + face model training

Arduino UNO r3+LCD1602+DHT11

【数据库】MySQL单表查询

Spark small case - RDD, spark SQL

Recursive call -- Enumeration of permutations

数据孤岛是什么?为什么2022年仍然存在数据孤岛?

Go reflection rule
随机推荐
Installation and deployment of Flink and wordcount test
New terminal play method: script guidance independent of technology stack
MySQL time function query
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
Innovation training (XI) airline ticket crawling company information
No such file or directory problem while executing shell
leetcode001--返回和为target的数组元素的下标
Innovation training (IV) preliminary preparation - server
Recommended scheme of national manufactured electronic components
Repair of self calibration SPC failure of Tektronix oscilloscope dpo3054
io. Platform. packageRoot; // ignore: deprecated_ Member_ use
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
AWS eks add cluster user or Iam role
Leetcode001 -- returns the subscript of the array element whose sum is target
Innovation training (II) task division
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
Code007 -- determine whether the string in parentheses matches
Recommended scheme for national production of electronic components of wireless keyboard
MySQL queries users logged in for at least N consecutive days
Excel protects worksheets and workbooks from damage