当前位置:网站首页>请问海量数据如何去取最大的K个
请问海量数据如何去取最大的K个
2022-06-30 20:00:00 【兔云程序】
这可能是形而上学很有深度一个算法题目,因为这个会的人能天马行空设计出绝妙的算法,不会的人可能连题目都无处下手。海量数据top K问题,在互联网大厂的产品中到处体现出来,比如微信的计步软件,统计出K名,然后进行排序。
当然类似的题目还有有一亿个浮点数,如何找出其中最大的10000个。这里面其实涉及代码技能就是内存的处理以及数据的去重优化,把本来需要占大量内存空间的海量数据通过各种方法处理出来。以下有几种方法,包括最蠢和最明智的方案,在面试中可以供你吹水。
内存允许的情况下,直接全部排序
这可能是最直接简单粗暴的方法,但是你懂的,我们的前提是海量数据,这个方法是一种方案,但是肯定不靠谱的。全部排序从大到小排序之后,我们直接取头部的K个,方法需要消耗大量内存并且不高效,做了很多无用功,不建议使用。当然如果在口谈面试中,你大脑短路的时候可以先提出这个。
内存允许的情况下,分治法
其实分治的思想里就包括快排和归并排序。思想先分都是将数据不断的分成N份,治是找到每份数据中最大的K个数。
最小堆法(也叫局部淘汰法)
一种局部淘汰法。先读取前K个数,建立一个最小堆。然后将剩余的所有数字依次与最小堆的堆顶进行比较,如果小于或等于堆顶数据,则继续比较下一个;否则,删除堆顶元素,并将新数据插入堆中,重新调整最小堆。当遍历完全部数据后,最小堆中的数据即为最大的K个数。
时间复杂度为O(n+m^2)(其中m为K,比如10000)
边栏推荐
- Detailed explanation of specific methods and steps for TCP communication between s7-1500 PLCs (picture and text)
- QT :QAxObject操作Excel
- Tensorflow2.4实现RepVGG
- Jerry's question about long press boot detection [chapter]
- 为什么一定要从DevOps走向BizDevOps?
- 股票开户优惠链接,我如何才能得到?另外,手机开户安全么?
- SQL优化
- Jerry's long press reset [chapter]
- CADD课程学习(2)-- 靶点晶体结构信息
- CADD course learning (1) -- basic knowledge of drug design
猜你喜欢

杰理之触摸按键识别流程【篇】

微信小程序开发实战 云音乐

TorchDrug--药物属性预测

Lambda 表达式原理分析学习(2022.06.23)

Summary of operating system interview questions (updated from time to time)

Exness: the final value of US GDP unexpectedly accelerated to shrink by 1.6%

Great God detailed open source Buff gain Introduction 丨 Live

How to pass the PMP Exam quickly?

DEX file parsing - Method_ IDS resolution

Solve the problems of Devops landing in complex environment with various tools with full stack and full function solutions
随机推荐
All the important spark summit features were released here last night (with ultra clear video attached)
GeoServer installation
maya房子建模
Data intelligence - dtcc2022! China database technology conference is about to open
Heartbeat and DRBD configuration process
Client请求外部接口标准处理方式
GeoServer安装
信息学奥赛一本通 1362:家庭问题(family)
网上炒股开户安全嘛!?
杰理之检测灵敏度级别确定【篇】
Originpro 2021 with installation tutorial
Black apple server system installation tutorial, black apple installation tutorial, teach you how to install black apple in detail [easy to understand]
QT :QAxObject操作Excel
Big God explains open source buff gain strategy live broadcast
Golang应用 ━━ 安装、配置与使用hugo博客系统
VB的基本语法
杰理之检测灵敏度级别确定【篇】
Exness: the final value of US GDP unexpectedly accelerated to shrink by 1.6%
How to pass the PMP Exam quickly?
Detailed explanation of specific methods and steps for TCP communication between s7-1500 PLCs (picture and text)