当前位置:网站首页>Sword finger offer 41 Median in data stream
Sword finger offer 41 Median in data stream
2022-06-29 19:40:00 【Yake1965】
The finger of the sword Offer 41. Median in data stream
class MedianFinder {
PriorityQueue<Integer> left;
PriorityQueue<Integer> right;
public MedianFinder() {
left = new PriorityQueue<Integer>((a, b) -> b - a);
right = new PriorityQueue<Integer>();
}
public void addNum(int num) {
if(left.isEmpty() || num <= left.peek()){
// left Is empty or num <= Median ( Large root heap top element )
left.offer(num);
if(left.size() - right.size() > 1){
// Transfer small pile top to large pile
right.offer(left.poll());
}
} else {
right.offer(num);
if(right.size() > left.size()) {
left.offer(right.poll());
}
}
}
public double findMedian() {
return left.size() > right.size() ? left.peek() : (left.peek() + right.peek()) / 2.0;
}
}
class MedianFinder:
def __init__(self):
self.q = []
def addNum(self, num: int) -> None:
insort_left(self.q, num)
def findMedian(self) -> float:
n = len(self.q)
return (self.q[n // 2] + self.q[(n-1) // 2]) / 2
from sortedcontainers import SortedList
class MedianFinder:
def __init__(self):
self.sl = SortedList()
def addNum(self, num: int) -> None:
self.sl.add(num)
def findMedian(self) -> float:
n = len(self.sl)
return (self.sl[n // 2] + self.sl[(n-1)//2]) / 2
边栏推荐
- NLP 类问题建模方案探索实践
- Arm comprehensive computing solution redefines visual experience and powerfully enables mobile games
- Where is the win11 installation permission set? Win11 installation permission setting method
- 7.取消与关闭
- 小米笔试真题一
- 数据库是什么?数据库详细笔记!带你走进数据库~你想知道的这里都有!
- 3-3主機發現-四層發現
- 畫虎國手孟祥順數字藏品限量發售,隨贈虎年茅臺
- 聊聊eureka的delta配置
- jfinal中如何使用过滤器监控Druid监听SQL执行?
猜你喜欢

Creators foundation highlights in June

【U盘检测】为了转移压箱底的资料,买了个2T U盘检测仅仅只有47G~

虎符限币种提现 用户曲线出金即亏损

4-2 port banner information acquisition

Game maker Foundation presents: Valley of belonging

Installation and configuration of MariaDB

以其他组件为代价的性能提升不是好提升

MySQL remote connection

One hour to build a sample scenario sound network to release lingfalcon Internet of things cloud platform

4-1端口扫描技术
随机推荐
QC协议+华为FCP+三星AFC快充取电5V9V芯片FS2601应用
【U盘检测】为了转移压箱底的资料,买了个2T U盘检测仅仅只有47G~
Common knowledge of ECS security settings
nacos 问题
Physical verification LVS process and Technology (Part I)
乐鑫面试流程
With these four security testing tools, software security testing can be said so easy!
童年经典蓝精灵之百变蓝爸爸数字藏品中奖名单公布
14.04 million! Sichuan provincial human resources and social security department relational database and middleware software system upgrade procurement bidding!
@SneakyThrows注解
php实现 提取不重复的整数(编程题目能够最快的熟悉函数)
DAO 中存在的不足和优化方案
以其他组件为代价的性能提升不是好提升
[software testing] 01 -- software life cycle and software development model
构建增强现实移动应用程序的六款顶级工具
How important is it to make a silver K-line chart?
The concept and properties of mba-day26 number
3-3 host discovery - layer 4 discovery
技术保证质量,软件测试的这些测试方法你都掌握了吗?
14,04 millions! Appel d'offres pour la mise à niveau de la base de données relationnelle et du système logiciel Middleware du Département des ressources humaines et sociales de la province du Sichuan!