当前位置:网站首页>Sword finger offer 41. median in data flow
Sword finger offer 41. median in data flow
2022-07-29 00:10:00 【The harder you work, the luckier you are】
Violent solution , Every time I look up , Just sort them all
class MedianFinder:
def __init__(self):
self.res=[]
def addNum(self, num):
self.res.append(num)
def findMedian(self):
l=len(self.res)
self.res.sort()
if l%2:
return self.res[l//2]
else:
return (self.res[l//2]+self.res[l//2-1])/2Big pile top , Small top pile method .
First of all, understand the following articles
Python There is no big top pile , What shall I do? ?
Then take all the numbers as their opposite numbers , such , You can use a small top pile .
from heapq import *
class MedianFinder:
def __init__(self):
self.A = [] # Small cap pile , Save the larger half
self.B = [] # Big pile top , Save the smaller half
def addNum(self, num: int) -> None:
if len(self.A) != len(self.B):
heappush(self.A, num)
heappush(self.B, -heappop(self.A))
else:
heappush(self.B, -num)
heappush(self.A, -heappop(self.B))
def findMedian(self) -> float:
return self.A[0] if len(self.A) != len(self.B) else (self.A[0] - self.B[0]) / 2.0
author :jyd
link :https://leetcode.cn/problems/shu-ju-liu-zhong-de-zhong-wei-shu-lcof/solution/mian-shi-ti-41-shu-ju-liu-zhong-de-zhong-wei-shu-y/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .边栏推荐
- Word中的\n是什么?:^p
- 研发效能的道法术器
- 【C】 Drink soda and find a single dog
- Principle of meter skipping
- Use pytoch to quickly train the network model
- Intelligent trash can (VII) -- Introduction and use of sg90 steering gear (Pico implementation of raspberry pie)
- 1-8 basic use of props
- EN 12101-8:2011 smoke dampers for smoke and heat control systems - CE certification
- [TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming
- Real time data warehouse: meituan reviews Flink's real-time data warehouse application sharing
猜你喜欢

Leetcode60. 排列序列

Genomic DNA isolation Worthington ribonuclease A

Control fillet stroke materialshapedrawable

Exchange 2013 SSL certificate installation document

PowerCLi VMware vCenter 通过自建的PXE Server一键批量部署常规New-VM

Powercli VMware vCenter deploys conventional new VMS in batch through self built PXE server with one click

SAP temporary tablespace error handling

DoIP测试开发实践

Use pytoch to quickly train the network model

mysql索引失效的常见9种原因详解
随机推荐
Eight performance analysis indicators of effective supply chain management (Part 1)
失败率高达80%,数字化转型如何正确完成战略规划?
Zibo station construction guide (aigler)
Summary of wrong questions of software designers
EN 12101-8:2011 smoke dampers for smoke and heat control systems - CE certification
Please briefly describe the respective characteristics of list, set and map type sets (briefly describe three different inheritance methods)
EN 1935 building hardware. Single axis hinge - CE certification
ISO 13400(DoIP)标准解读
The failure rate is as high as 80%. How to correctly complete the strategic planning of digital transformation?
What do you need to bring with you for the NPDP exam? Stationery carrying instructions
Worthington - chemical properties and related studies of Worthington trypsin
【MySQL系列】MySQL数据库基础
Build SSM project with JSP as view parser
CMake 基础学习
EN 1873 assembly accessories for roofing - plastic single roof lamps - CE certification
Servlet运行原理_API详解_请求响应构造进阶之路(Servlet_2)
Android studio连接MySQL并完成简单的登录注册功能
Use pytoch to quickly train the network model
1-5 类式组件
GhostNets on Heterogeneous Devices via Cheap Operations
https://leetcode.cn/problems/shu-ju-liu-zhong-de-zhong-wei-shu-lcof/