当前位置:网站首页>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(); */
边栏推荐
- yocto 技术分享第四期:自定义增加软件包支持
- MySQL root user needs sudo login
- Opencv notes 20 PCA
- Which language should I choose to program for single chip microcomputer
- On the problem of reference assignment to reference
- The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
- QT detection card reader analog keyboard input
- LeetCode - 673. 最长递增子序列的个数
- Dictionary tree prefix tree trie
- When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
猜你喜欢

Not many people can finally bring their interests to college graduation

Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)

When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn

Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction

Yocto Technology Sharing Phase 4: Custom add package support

yocto 技术分享第四期:自定义增加软件包支持

openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍

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

Working mode of 80C51 Serial Port

The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
随机推荐
MySQL root user needs sudo login
Development of intelligent charging pile (I): overview of the overall design of the system
Opencv notes 20 PCA
Circular queue related design and implementation reference 1
Tensorflow built-in evaluation
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
Crash工具基本使用及实战分享
Vscode markdown export PDF error
(1) What is a lambda expression
Stm32f04 clock configuration
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
It is difficult to quantify the extent to which a single-chip computer can find a job
Which language should I choose to program for single chip microcomputer
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
Leetcode bit operation
LeetCode - 933 最近的请求次数
STM32 general timer 1s delay to realize LED flashing
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer