当前位置:网站首页>由排行榜实时更新想到的数状数值
由排行榜实时更新想到的数状数值
2022-07-08 00:01:00 【lixia0417mul2】
排行榜常规实现
1.单机:使用普通数值存储每个用户的分数,更新某个用户的分数的时间复杂度是O(1),查询用户分数排行榜的时间复杂度是O(NlogN),如果总共有100000个用户,有1000个用户在同时更新,那么时间复杂度是O(1000) + 1000 * O(100000 * log100000) = 1800000100=1800w次操作
2.分布式: 分布式一般是基于redis的zset有序集合实现,每个用户的分数时每个有序集合元素的score的值,redis的zset集合的底层数据结构是跳表,更新分数操作的时间复杂度是O(logN),所以使用跳表结构来实现排行榜,10w用户同时有1000用户在更新时,时间复杂度只是(1000 * log100000) = 180000=18w次操作.
由此可见,如果是单机或者分布式要实现实时更新排行榜的功能,使用有序数组(比如基于跳表结构实现的有序数组)是可以满足要求的,时间复杂度都是O(logN),那还有没有一种结构也可以满足要求,并且空间复杂度仅仅是O(N)呢,答案是有
- 树状数值首先是一个数组的结构,数组中的元素有可能存放着前面几个元素的前缀和,也有可能仅仅存放着自身的元素值,至于序号为x的元素存放的值是起前面多少个元素的前缀和由lowbit(x)函数决定,lowbit(x) = x & (-x)也即x的二进制为1的最低位位数决定,比如lowbit(6) = lowbit(110) = 2, lowbit(3)=lowbit(011)=1等
- 对于更新操作来说,比如更新x序号位置的分数值,他不仅仅只是更新x位置的元素值,他还要更新那些包含了x这个前缀值的后面的那些序号的值,假设总共有16个数组元素,现在要更新索引位置为6的元素值,除了更新5这个位置的元素值外,还要更新0b110 + lowbit(110) = 0b1000=8这个位置的值,此外还要继续更新0b1000+lowbit(0b1000) = 0x10000=16这个位置的值(不过这个位置的值大于了最大数组长度,不需要更新),相当于要一直加上lowbit的值向后更新对应索引的元素值
- 对于获取前缀和或者区间和的操作,由于当前序号的元素值有可能并没有包括他前面所有的元素的元素值,所以需要把前面的值加上,比如要获取一直到索引=6的前缀和,需要加上索引6的元素值,再加上0b110 - lowbit(0b110) = 0b100索引的元素值,还要继续加上0b100-lowbit(0b100) = 0索引的元素值,这样才是完整的前缀和
对于树状数组来说,更新和求任意的区间和操作的时间复杂度都是O(logN),性能相当理想,排行榜的场景也可以转成使用树状数组实现,不过存放的含义要改变以下,数组中存放的分数等于某个索引值的用户数,此时当我从改变了分数的时候比如从oldScore变成了newScore,相当于索引为oldScore的用户数减去1,走前面的更新流程,然后索引为newScore的用户数加上1,也走前面的更新流程,最后我计算出从索引oldScore到newScore的区间和,这个区间和就是这个用户的排行榜名次变动的值
边栏推荐
- Gnuradio operation error: error thread [thread per block [12]: < block OFDM_ cyclic_ prefixer(8)>]: Buffer too small
- Redis 主从复制
- Running OFDM in gnuradio_ RX error: gr:: Log: info: packet_ headerparser_ b0 - Detected an invalid packet at item ××
- 腾讯游戏客户端开发面试 (Unity + Cocos) 双重轰炸 社招6轮面试
- On the concept and application of filtering in radar signal processing
- How does Matplotlib and PIL image integrate and save multiple pictures into one picture
- 2022 low voltage electrician examination content and low voltage electrician simulation examination question bank
- STM32GPIO口的工作原理
- Scalar / vector / matrix derivation method
- 跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
猜你喜欢
break net
Redis master-slave replication
2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills
nacos-微服务网关Gateway组件 +Swagger2接口生成
Redis cluster
用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Scalar / vector / matrix derivation method
Understanding of prior probability, posterior probability and Bayesian formula
随机推荐
Break algorithm --- map
The beauty of Mathematics -- the principle of fine Fourier transform
Problems of font legend and time scale display of MATLAB drawing coordinate axis
About how USRP sets the sampling frequency below the minimum sampling frequency reached by the hardware
Solve the error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
After modifying the background of jupyter notebook and adding jupyterthemes, enter 'JT -l' and the error 'JT' is not an internal or external command, nor a runnable program
Gnuradio3.9.4 create OOT module instances
Frequency probability and Bayesian probability
2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
项目经理有必要考NPDP吗?我告诉你答案
2022 operation certificate examination for main principals of hazardous chemical business units and main principals of hazardous chemical business units
MySQL数据库(2)
Guojingxin center "APEC education +" Shanghai Jiaotong University Japan Cooperation Center x Fudan philosophy class "Zhe Yi" 2022 New Year greetings
2021-03-06 - play with the application of reflection in the framework
ROS 问题(topic types do not match、topic datatype/md5sum not match、msg xxx have changed. rerun cmake)
pb9.0 insert ole control 错误的修复工具
Leetcode notes No.21
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
npm 內部拆分模塊
NPM internal split module