当前位置:网站首页>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(); */
边栏推荐
- Drive and control program of Dianchuan charging board for charging pile design
- 2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
- The underlying principle of vector
- Dynamic layout management
- Swing transformer details-1
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- 4G module IMEI of charging pile design
- 01仿B站项目业务架构
- Application of 51 single chip microcomputer timer
- Modelcheckpoint auto save model
猜你喜欢
要選擇那種語言為單片機編寫程序呢
Dictionary tree prefix tree trie
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
使用密钥对的形式连接阿里云服务器
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
2021-10-28
Blue Bridge Cup for migrant workers majoring in electronic information engineering
QT is a method of batch modifying the style of a certain type of control after naming the control
LeetCode - 933 最近的请求次数
随机推荐
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
2020-08-23
01 business structure of imitation station B project
About windows and layout
2021-10-27
4G module at command communication package interface designed by charging pile
Gpiof6, 7, 8 configuration
JS基础-原型原型链和宏任务/微任务/事件机制
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
Opencv interview guide
MySQL root user needs sudo login
我想各位朋友都应该知道学习的基本规律就是:从易到难
03 FastJson 解决循环引用
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Design of charging pile mqtt transplantation based on 4G EC20 module
My notes on intelligent charging pile development (II): overview of system hardware circuit design
没有多少人能够最终把自己的兴趣带到大学毕业上
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
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)