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

边栏推荐
- Pytorch based generation countermeasure Network Practice (7) -- using pytorch to build SGAN (semi supervised GaN) to generate handwritten digits and classify them
- Mongodb series window environment deployment configuration
- Build your own PE manually from winpe of ADK
- Correlation analysis related knowledge
- [ahoi2005] route planning
- Svn commit error after deleting files locally
- Postman自动化接口测试
- Wechat applet -picker component is repackaged and the disabled attribute is added -- below
- Bug memory management
- Win10 home vs pro vs enterprise vs enterprise LTSC
猜你喜欢

Build your own PE manually from winpe of ADK

2021-10-09

Freefilesync folder comparison and synchronization software

Pointer

Stream常用操作以及原理探索

How to call self written functions in MATLAB

33. Use rgbd camera for target detection and depth information output

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

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

Global variable vs local variable
随机推荐
免费的机器学习数据集网站(6300+数据集)
C language ---getchar() and putchar()
Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
Insect operator overloaded a fun
Comparison of disk partition modes (MBR and GPT)
Es common grammar I
团队管理的最关键因素
Wechat applet - bind and prevent event bubble catch
2021-10-18 character array
Memory considerations under bug memory management
Half search, character array definition, character array uses D11
On insect classes and objects
Range of types
微信小程序注册指引
Luogu p4145 seven minutes of God created questions 2 / Huashen travels around the world
d的is表达式
9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》
Svn commit error after deleting files locally
Installation and uninstallation of MySQL software for windows
Lucky numbers in the matrix
https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/