当前位置:网站首页>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(); */
边栏推荐
- LeetCode - 933 最近的请求次数
- LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
- Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
- 嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
- Swing transformer details-2
- 手机都算是单片机的一种,只不过它用的硬件不是51的芯片
- Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
- 4G module board level control interface designed by charging pile
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- Timer and counter of 51 single chip microcomputer
猜你喜欢

LeetCode - 919. 完全二叉树插入器 (数组)

Quelle langue choisir pour programmer un micro - ordinateur à puce unique

Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program

Dictionary tree prefix tree trie

新系列单片机还延续了STM32产品家族的低电压和节能两大优势

LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)

JS基础-原型原型链和宏任务/微任务/事件机制

Interruption system of 51 single chip microcomputer

2. Elment UI date selector formatting problem

03 fastjason solves circular references
随机推荐
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
My notes on intelligent charging pile development (II): overview of system hardware circuit design
About windows and layout
LeetCode - 933 最近的请求次数
Tensorflow2.0 save model
Screen display of charging pile design -- led driver ta6932
Yocto Technology Sharing Phase 4: Custom add package support
Working mode of 80C51 Serial Port
Simple use of MySQL (addition, deletion, modification and query)
On the problem of reference assignment to reference
Stm32f04 clock configuration
2020-08-23
Swing transformer details-2
Vscode markdown export PDF error
Toolbutton property settings
Notes on C language learning of migrant workers majoring in electronic information engineering
Which language should I choose to program for single chip microcomputer
A lottery like scissors, stone and cloth (C language)
03 FastJson 解决循环引用
Application of external interrupts