当前位置:网站首页>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(); */
边栏推荐
- 03 fastjason solves circular references
- 使用密钥对的形式连接阿里云服务器
- Stm32f04 clock configuration
- QT self drawing button with bubbles
- My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
- Opencv notes 17 template matching
- 学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
- About windows and layout
- Adaptiveavgpool1d internal implementation
- Basic knowledge of communication interface
猜你喜欢

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

Gpiof6, 7, 8 configuration

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

Opencv histogram equalization

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

Opencv notes 20 PCA

Oracle database SQL statement execution plan, statement tracking and optimization instance

RESNET code details

单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇

It is difficult to quantify the extent to which a single-chip computer can find a job
随机推荐
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Yocto technology sharing phase IV: customize and add software package support
LeetCode - 705 设计哈希集合(设计)
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
Development of intelligent charging pile (I): overview of the overall design of the system
STM32 running lantern experiment - library function version
Opencv notes 17 template matching
Basic knowledge of communication interface
Yocto Technology Sharing Phase 4: Custom add package support
Wireshark use
QT is a method of batch modifying the style of a certain type of control after naming the control
Vgg16 migration learning source code
Opencv histogram equalization
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
It is difficult to quantify the extent to which a single-chip computer can find a job
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
Swing transformer details-1
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)