当前位置:网站首页>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(); */
边栏推荐
- pycharm 无法引入自定义包
- Cases of OpenCV image enhancement
- Stm32 NVIC interrupt priority management
- openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
- 我想各位朋友都应该知道学习的基本规律就是:从易到难
- Modelcheckpoint auto save model
- Opencv note 21 frequency domain filtering
- 学习开发没有捷径,也几乎不存在带路会学的快一些的情况
- Application of 51 single chip microcomputer timer
- Drive and control program of Dianchuan charging board for charging pile design
猜你喜欢
El table X-axis direction (horizontal) scroll bar slides to the right by default
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
Gpiof6, 7, 8 configuration
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
LeetCode - 933 最近的请求次数
Swing transformer details-2
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Basic knowledge of communication interface
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
随机推荐
YOLO_ V1 summary
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
单片机学到什么程度能找到工作,这个标准不好量化
My 4G smart charging pile gateway design and development related articles
Sending and interrupt receiving of STM32 serial port
[keil5 debugging] warning:enumerated type mixed with other type
01仿B站项目业务架构
Drive and control program of Dianchuan charging board for charging pile design
2021-10-27
Yocto technology sharing phase IV: customize and add software package support
Working mode of 80C51 Serial Port
Toolbutton property settings
STM32 general timer 1s delay to realize LED flashing
Simulate mouse click
Window maximum and minimum settings
There is no specific definition of embedded system
(1) 什么是Lambda表达式
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
(1) What is a lambda expression
Serial port programming