当前位置:网站首页>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(); */
边栏推荐
- Dictionary tree prefix tree trie
- 2020-08-23
- 学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
- LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
- ADS simulation design of class AB RF power amplifier
- Installation and removal of MySQL under Windows
- Screen display of charging pile design -- led driver ta6932
- yocto 技術分享第四期:自定義增加軟件包支持
- 4G module initialization of charge point design
- 01仿B站项目业务架构
猜你喜欢

Opencv notes 17 template matching

SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers

Education is a pass and ticket. With it, you can step into a higher-level environment

当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学

要選擇那種語言為單片機編寫程序呢

Opencv histogram equalization

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

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

LeetCode - 706 设计哈希映射(设计) *

03 fastjason solves circular references
随机推荐
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
LeetCode - 673. 最长递增子序列的个数
03 FastJson 解决循环引用
Retinaface: single stage dense face localization in the wild
Working mode of 80C51 Serial Port
Installation and removal of MySQL under Windows
STM32 general timer 1s delay to realize LED flashing
Simulate mouse click
Blue Bridge Cup for migrant workers majoring in electronic information engineering
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
El table X-axis direction (horizontal) scroll bar slides to the right by default
Dynamic layout management
单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
Application of external interrupts
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
Cases of OpenCV image enhancement
(1) 什么是Lambda表达式
It is difficult to quantify the extent to which a single-chip computer can find a job
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
Opencv notes 20 PCA