当前位置:网站首页>LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
2022-07-03 09:20:00 【三岁就很萌@D】


class MedianFinder {
/** initialize your data structure here. */
private PriorityQueue<Integer> minHeap;//小顶堆
private PriorityQueue<Integer> maxHeap;//大顶堆
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(); */
边栏推荐
- Retinaface: single stage dense face localization in the wild
- LeetCode - 673. 最长递增子序列的个数
- STM32 interrupt switch
- Liquid crystal display
- Windows下MySQL的安装和删除
- Emballage automatique et déballage compris? Quel est le principe?
- An executable binary file contains more than machine instructions
- LeetCode - 919. 完全二叉树插入器 (数组)
- is_ power_ of_ 2 judge whether it is a multiple of 2
- 嵌入式系统没有特别明确的定义
猜你喜欢

Basic knowledge of communication interface

I think all friends should know that the basic law of learning is: from easy to difficult

How does the memory database give full play to the advantages of memory?

Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction

Stm32-hal library learning, using cubemx to generate program framework

Notes on C language learning of migrant workers majoring in electronic information engineering

An executable binary file contains more than machine instructions

学习开发没有捷径,也几乎不存在带路会学的快一些的情况

Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?

Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
随机推荐
Interruption system of 51 single chip microcomputer
Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
Stm32 NVIC interrupt priority management
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
我想各位朋友都应该知道学习的基本规律就是:从易到难
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
uniapp 实现微信小程序全局分享及自定义分享按钮样式
Leetcode 300 最长上升子序列
学习开发没有捷径,也几乎不存在带路会学的快一些的情况
Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
Liquid crystal display
要选择那种语言为单片机编写程序呢
el-table X轴方向(横向)滚动条默认滑到右边
Design of charging pile mqtt transplantation based on 4G EC20 module
Uniapp realizes global sharing of wechat applet and custom sharing button style
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
Serial communication based on 51 single chip microcomputer
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
Pymssql controls SQL for Chinese queries