当前位置:网站首页>Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
2022-07-03 10:06:00 【Cute at the age of three @d】


class MedianFinder {
/** initialize your data structure here. */
private PriorityQueue<Integer> minHeap;// Small cap pile
private PriorityQueue<Integer> maxHeap;// Big pile top
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(); */
边栏推荐
- I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
- 03 FastJson 解决循环引用
- Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
- 2. Elment UI date selector formatting problem
- Exception handling of arm
- Opencv gray histogram, histogram specification
- YOLO_ V1 summary
- Windows下MySQL的安装和删除
- Application of 51 single chip microcomputer timer
- Education is a pass and ticket. With it, you can step into a higher-level environment
猜你喜欢

没有多少人能够最终把自己的兴趣带到大学毕业上

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*

Opencv feature extraction sift

应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机

学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里

openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍

学习开发没有捷径,也几乎不存在带路会学的快一些的情况

RESNET code details

Not many people can finally bring their interests to college graduation

LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
随机推荐
03 FastJson 解决循环引用
LeetCode - 705 设计哈希集合(设计)
Sending and interrupt receiving of STM32 serial port
Retinaface: single stage dense face localization in the wild
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
Google browser plug-in recommendation
03 FastJson 解决循环引用
Drive and control program of Dianchuan charging board for charging pile design
YOLO_ V1 summary
Timer and counter of 51 single chip microcomputer
Installation and removal of MySQL under Windows
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
El table X-axis direction (horizontal) scroll bar slides to the right by default
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
Opencv gray histogram, histogram specification
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
没有多少人能够最终把自己的兴趣带到大学毕业上
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software