当前位置:网站首页>LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
2022-07-03 09:20:00 【三岁就很萌@D】


class MedianFinder {
/** initialize your data structure here. */
private PriorityQueue<Integer> minHeap;//小顶堆
private PriorityQueue<Integer> maxHeap;//大顶堆
public MedianFinder() {
minHeap = new PriorityQueue<Integer>(new Comparator<Integer>(){
public int compare(Integer i1,Integer i2){
return i1.compareTo(i2);
}
});
maxHeap = new PriorityQueue<Integer>(new Comparator<Integer>(){
public int compare(Integer i1,Integer i2){
return i2.compareTo(i1);
}
});
}
public void makeBalance(){
if(minHeap.size() > maxHeap.size()){
maxHeap.offer(minHeap.poll());
}
else if(maxHeap.size() > minHeap.size() + 1){
minHeap.offer(maxHeap.poll());
}
}
public void addNum(int num) {
if(maxHeap.size() == 0 || num <= maxHeap.peek())
maxHeap.offer(num);
else
minHeap.offer(num);
makeBalance();
}
public double findMedian() {
int k = minHeap.size() + maxHeap.size();
return k % 2 == 0 ? ((double) minHeap.peek() + maxHeap.peek()) / 2 : maxHeap.peek();
}
}
/** * Your MedianFinder object will be instantiated and called as such: * MedianFinder obj = new MedianFinder(); * obj.addNum(num); * double param_2 = obj.findMedian(); */
边栏推荐
猜你喜欢

Oracle database SQL statement execution plan, statement tracking and optimization instance

The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving

LeetCode - 673. 最长递增子序列的个数

嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走

Uniapp realizes global sharing of wechat applet and custom sharing button style

How does the memory database give full play to the advantages of memory?

For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer

Oracle数据库 SQL语句执行计划、语句跟踪与优化实例

yocto 技术分享第四期:自定义增加软件包支持

GPIO port details, Hal library operation keys
随机推荐
I think all friends should know that the basic law of learning is: from easy to difficult
Project cost management__ Topic of comprehensive calculation
Retinaface: single stage dense face localization in the wild
Oracle database SQL statement execution plan, statement tracking and optimization instance
03 fastjason solves circular references
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
2020-08-23
The third paper of information system project manager in soft examination
自動裝箱與拆箱了解嗎?原理是什麼?
Project cost management__ Cost management technology__ Article 7 completion performance index (tcpi)
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
Simple use of MySQL (addition, deletion, modification and query)
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
2. Elment UI date selector formatting problem
Swing transformer details-2
Project cost management__ Cost management technology__ Article 8 performance review
Project cost management__ Cost management technology__ Article 6 prediction