当前位置:网站首页>面试之 top k问题
面试之 top k问题
2022-07-03 16:17:00 【左海峰博客】
从大量数据里查找前多少大或者小的数据。
第一个例子:有4g内存,有1千万ipv4的地址。找其中出现次数最多的前100个。 设ipv4地址192.168.111.222。以字符串15个char,每个char两个字节。每个地址30字节存储。1千万就是3亿字节。 3亿字节 = 0.3g。所以内存够用。ipv4如果转为int存储。内存更小。
1.在内存够用的前提下:
首先进行统计。 再排序。
做出一个k,v的结构。每一个ip出现多少次。可以通过hashmap或者数组进行映射。 统计后排序找最大或最小。(这里的排序与堆排序有所不同) 比如找前100大的,我们假设先把hashmap或数组中的前100个当做最大的,只要记录这里边最小的那个。然后与新的数作比较。 大就留下把记录的那个丢掉,小的话就过。 借助顶堆降低复杂度。前一百大使用小顶堆,前一百小使用大顶堆。 比如找前100大的,可以创建一个100个节点的小顶堆,在维护小顶堆的时候与堆排序有所不同。堆排序是将所有的节点构造成一个堆,然后维护好之后呢他是每次将堆顶和最后一个节点互换位置。互换后将最后那个节点去掉。 这里的排序是维护一个100节点的堆。这个堆的大小不会改变。然后就是遍历咱们统计的那个hashmap或者是数组。先把前100个近似看作是出现最多的做成一个小顶堆。然后开始遍历第101个,每次与堆顶比较。如果比堆顶大。就与堆顶进行交换。交换完成后维护小顶堆。维护好就比较第102个数。这样依次比较下去就能得到前100个出现次数最多的堆。 复杂度nlogm,n代表这1千万,m代表前100。
2.内存不够:
给了100g的文本。文本出现如下两列,一个是ip地址,一个是url。找出url被不同的ip访问的次数的前100多的。 给了4g的机器,无限的硬盘。
1.2.3.4 baidu.com
1.2.3.4 taobao.com
1.2.3.3 taobao.com
1.2.2.2 baidu.com
1.2.2.2 baidu.com
这里就是baidu是2个,taobao也是2个。
1.有没有更多的机器。可以多机器处理,每个机器处理一部分。
2.借用现有的软件比如mysql。通过sql语句查出来。
3.借助硬盘排序。按照域名命名文件。把访问这个域名的ip放进来,或者使用hash,对域名进行对n求余,将相同的域名数据放入到同一个文本便于统计。 统计时读取文件,不需要把所有的文件都放在内存中,读一个文件把域名对应独立ip的个数统计一下。 统计完成之后又可以借助顶堆计算。
边栏推荐
- Nine ways to define methods in scala- Nine ways to define a method in Scala?
- 工资3000,靠“视频剪辑”月入40000:会赚钱的人,从不靠拼命!
- Embedded development: seven reasons to avoid open source software
- 0214-27100 a day with little fluctuation
- Stm32f103c8t6 firmware library lighting
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
- Go language self-study series | if else if statement in golang
- One article takes you to understand machine learning
- Remote file contains actual operation
- [combinatorics] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)
猜你喜欢

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)

Function introduction of JMeter thread group

Uploads labs range (with source code analysis) (under update)

Please be prepared to lose your job at any time within 3 years?
![[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display](/img/46/c7f566f8fd46d383b055582d680bb7.png)
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)

Salary 3000, monthly income 40000 by "video editing": people who can make money never rely on hard work!
![[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix](/img/d6/3c21c25f1c750f17aeb871124e80f4.png)
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix

初试scikit-learn库
随机推荐
工资3000,靠“视频剪辑”月入40000:会赚钱的人,从不靠拼命!
[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
记一次jar包冲突解决过程
How can technology managers quickly improve leadership?
Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
[combinatorics] combinatorial identity (sum of combinatorial identity products 1 | sum of products 1 proof | sum of combinatorial identity products 2 | sum of products 2 proof)
探索Cassandra的去中心化分布式架构
Golang 装饰器模式以及在NSQ中的使用
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
Go language self-study series | if else if statement in golang
uploads-labs靶场(附源码分析)(更新中)
Getting started with Message Oriented Middleware
深度学习之三维重建
Construction practice camp - graduation summary of phase 6
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
潘多拉 IOT 开发板学习(HAL 库)—— 实验5 外部中断实验(学习笔记)
Three dimensional reconstruction of deep learning
MB10M-ASEMI整流桥MB10M