当前位置:网站首页>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
边栏推荐
- With these four security testing tools, software security testing can be said so easy!
- Tiger painter mengxiangshun's digital collection is on sale in limited quantities and comes with Maotai in the year of the tiger
- 云服务器的安全设置常识
- 4-2 port banner information acquisition
- JVM(2) 垃圾回收
- Nacos problem
- 【观察】软通动力刘天文:拥抱变化“顺势而为”,做中国数字经济“使能者”...
- The era of data security solutions
- 【网络方向实训】-企业园区网络设计-【Had Done】
- Arm comprehensive computing solution redefines visual experience and powerfully enables mobile games
猜你喜欢
Physical verification LVS process and Technology (Part I)
Luoqingqi: has high-end household appliances become a red sea? Casati took the lead in breaking the game
mysql远程连接
Flutter 调用百度地图APP实现位置搜索、路线规划
【网络方向实训】-企业园区网络设计-【Had Done】
KDD 2022 | 协同过滤中考虑表征对齐和均匀性
MSYQL, redis, mongodb visual monitoring tool grafana
La collection numérique Meng xiangshun, artiste national du tigre peint, est disponible en quantité limitée et est offerte avec Maotai de l'année du tigre
idea中方法上没有小绿色三角
一个mysql里有3306端口下,一个mysql有20多个数据库,怎么一键备份20多个数据库,做系统备份,防止数据误删除?
随机推荐
MSYQL, redis, mongodb visual monitoring tool grafana
【网络方向实训】-企业园区网络设计-【Had Done】
[笔记]再笔记--边干边学Verilog HDL –008
Inception 新结构 | 究竟卷积与Transformer如何结合才是最优的?
After CDN is added to the website, the Font Icon reports an error access control allow origin
技术保证质量,软件测试的这些测试方法你都掌握了吗?
JVM(4) 字节码技术+运行期优化
[observation] softcom power liutianwen: embrace change and "follow the trend" to become an "enabler" of China's digital economy
4-2 port banner information acquisition
正则表达式系列之手机号码正则
Have you mastered all the testing methods of technology to ensure quality and software testing?
领先11%,华为云天筹AI求解器再度登顶国际权威榜单
软件测试逻辑覆盖相关理解
What about frequent network disconnection of win11 system? Solution to win11 network instability
Escape and March, the "two-sided Jianghu" of temporary food
Arm 全面计算解决方案重新定义视觉体验强力赋能移动游戏
有了这4个安全测试工具,对软件安全测试say so easy!
测试方法学习
终于,进亚马逊了~
How is the combination of convolution and transformer optimal?