当前位置:网站首页>Sword finger offer 40.41 Sort (medium)
Sword finger offer 40.41 Sort (medium)
2022-06-26 14:14:00 【hedgehog:】
40.
subject :

idea : Prioritize , Back again .
class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
Arrays.sort(arr);
int[] res = new int[k];
for(int i=0;i<arr.length;i++){
if(k-->0)
res[i]=arr[i];
}
return res;
}
}result :

41.


idea : Think of bubbling , choice , Heap sorting can be used to sort the elements to one final position each time , Use bubble sort to find half of bubbles each time , But the result timed out .
idea : You can use heap sort , Faster .
In fact, it's not just fast , You can use two heaps , The big top pile stores the smaller half , The small top stack stores half the larger number , And make the length of the big top pile >= Small cap pile , Equal length , Put it in the big top pile , In this way, parity can be determined first , Then get the median by getting the value of one or two heap tops .
Code :
class MedianFinder {
Queue<Integer> A, B;// Use priority queues ( Default small top heap , The large top heap needs to customize the comparison criteria )
public MedianFinder() {
A = new PriorityQueue<>(); // Small cap pile , Save the larger half
// B = new PriorityQueue<>((x, y) -> (y - x)); // Big pile top , Save the smaller half
B = new PriorityQueue<>(Comparator.reverseOrder()); // Big pile top , Save the smaller half
}
public void addNum(int num) {
if(A.size() > B.size()) { // before , Odd length -> Add to B in , Make even A The length of =B The length of
A.add(num);
B.add(A.poll());
} else { // before , The length is even -> Add to A in , Make odd A The length of >B The length of
B.add(num);
A.add(B.poll());
}
}
public double findMedian() {
return A.size() > B.size() ? A.peek() : (A.peek() + B.peek()) / 2.0;
}
}
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/result :

边栏推荐
- hands-on-data-analysis 第三单元 模型搭建和评估
- [ahoi2005] route planning
- Difference between classification and regression
- Assert and constd13
- C language | the difference between heap and stack
- Jenkins build prompt error: eacces: permission denied
- Installation and uninstallation of MySQL software for windows
- ICML 2022 | limo: a new method for rapid generation of targeted molecules
- Taishan Office Technology Lecture: four cases of using bold font
- Pycharm远程连接服务器来跑代码
猜你喜欢

Embedded virlog code running process

Exercise set 1

Relevant knowledge of information entropy

爱可可AI前沿推介(6.26)

Sword finger offer 05.58 Ⅱ string

Jenkins build prompt error: eacces: permission denied

常用控件及自定义控件

windows版MySQL软件的安装与卸载

Hard (magnetic) disk (I)

Never use redis expired monitoring to implement scheduled tasks!
随机推荐
GEE——全球人类居住区网格数据 1975-1990-2000-2014
【系统分析师之路】第十五章 复盘数据库系统(数据库案例分析)
Luogu p4145 seven minutes of God created questions 2 / Huashen travels around the world
近期比较重要消息
程序员必备,一款让你提高工作效率N倍的神器uTools
Knowledge about the determination coefficient R2 and the relationship with the correlation coefficient
Logical operation
Applicable and inapplicable scenarios of mongodb series
C language | the difference between heap and stack
C language ---getchar() and putchar()
Es common grammar I
ICML 2022 | limo: a new method for rapid generation of targeted molecules
Obtain information about hard disk and volume or partition (capacity, ID, volume label name, etc.)
How to call self written functions in MATLAB
Free machine learning dataset website (6300+ dataset)
微信小程序注册指引
ICML 2022 | LIMO: 一种快速生成靶向分子的新方法
RISC-V 芯片架构新规范
A must for programmers, an artifact utools that can improve your work efficiency n times
PHP非对称加密算法(RSA)加密机制设计
https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/