当前位置:网站首页>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 :

边栏推荐
- A must for programmers, an artifact utools that can improve your work efficiency n times
- Knowledge about adsorption
- Record: why is there no lightning 4 interface graphics card docking station and mobile hard disk?
- Common operation and Principle Exploration of stream
- 虫子 内存管理 下 内存注意点
- Difference between classification and regression
- 2021-10-09
- 布局管理器~登录界面的搭建实例
- Hard (magnetic) disk (II)
- Is it safe to open a securities account? Is there any danger
猜你喜欢

Exercise set 1

9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》

Gartner 2022年顶级战略技术趋势报告

Wechat applet -picker component is repackaged and the disabled attribute is added -- above

C language | file operation and error prone points

使用 Performance 看看浏览器在做什么

Self created notes (unique in the whole network, continuously updated)

Codeforces Global Round 21A~D

Hard (magnetic) disk (I)

Tips for using nexys A7 development board resources
随机推荐
windows版MySQL软件的安装与卸载
Formal parameters vs actual parameters
Sword finger offer 06.24.35 Linked list
虫子 类和对象 中
CF676C Vasya and String
Reprint - easy to use wechat applet UI component library
Range of types
Wechat applet - bind and prevent event bubble catch
d的is表达式
Introduction to 26 papers related to CVPR 2022 document image analysis and recognition
Niuke challenge 48 e speed instant forwarding (tree over tree)
Svn commit error after deleting files locally
Wechat applet -picker component is repackaged and the disabled attribute is added -- below
Record: why is there no lightning 4 interface graphics card docking station and mobile hard disk?
Related knowledge of libsvm support vector machine
同花顺股票开户选哪个证券公司是比较好,比较安全的
[hcsd application development training camp] one line of code second cloud evaluation article - experience from the experiment process
Gartner 2022年顶级战略技术趋势报告
9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》
Sword finger offer 05.58 Ⅱ string
https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/