当前位置:网站首页>leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
2022-07-25 15:44:00 【白速龙王的回眸】

分析
前缀和记录每个的权重比
然后二分一个随机数,看看落在哪个区间,就属于哪个数
普通随机
class Solution:
def __init__(self, w: List[int]):
self.choices = []
for i, v in enumerate(w):
self.choices.extend([i] * v)
#print(self.choices)
def pickIndex(self) -> int:
l, r = 0, len(self.choices) - 1
while l < r:
mid = (l + r) // 2
rr = random.random()
if rr <= 0.5:
l = mid + 1
else:
r = mid
return self.choices[l]
# Your Solution object will be instantiated and called as such:
# obj = Solution(w)
# param_1 = obj.pickIndex()
使用了多次随机,伪随机数多次混合精度丢失
前缀和二分 只用一次随机
class Solution:
def __init__(self, w: List[int]):
self.preSum = list(accumulate(w))
self.tot = sum(w)
def pickIndex(self) -> int:
rr = random.randint(1, self.tot)
return bisect_left(self.preSum, rr)
# Your Solution object will be instantiated and called as such:
# obj = Solution(w)
# param_1 = obj.pickIndex()
总结
按权重随机选择 = 前缀和 + 二分看看落在哪个区域
边栏推荐
- SVD singular value decomposition derivation and application and signal recovery
- Solve the vender-base.66c6fc1c0b393478adf7.js:6 typeerror: cannot read property 'validate' of undefined problem
- R语言ggplot2可视化线图(line)、自定义配置标题文本相关内容颜色和图例(legend)颜色相匹配(和分组线图的颜色相匹配、match colors of groups)
- Matlab simulation of BPSK modulation system (1)
- Sword finger offer | number of 1 in binary
- 没错,请求DNS服务器还可以使用UDP协议
- Leetcode - 359 log rate limiter (Design)
- Reasons for data format conversion when matlab reads the displayed image
- Baseband simulation system experiment of 4pam in Gaussian channel and Rayleigh channel
- 记得那两句话
猜你喜欢

面试突击:为什么 TCP 需要 3 次握手?

基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现

MySQL—常用SQL语句整理总结

推荐系统-协同过滤在Spark中的实现

No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac

Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式

I interviewed 8 companies and got 5 offers in a week. Share my experience

Leetcode - 379 telephone directory management system (Design)

Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network

MySQL - user and permission control
随机推荐
Where is there a demo to set up the flex CDC to draw the number of MySQL?
LeetCode - 677 键值映射(设计)*
How to realize page inclusion
SVD singular value decomposition derivation and application and signal recovery
Activity review | July 6 Anyuan AI X machine heart series lecture No. 2 | MIT professor Max tegmark shares "symbiotic evolution of human and AI"
Baseband simulation system experiment of 4pam in Gaussian channel and Rayleigh channel
mysql 查看是否锁表
Pytoch learning notes -- Summary of common functions 3
电阻电路的等效变化(Ⅱ)
行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
HDD Hangzhou station · harmonyos technical experts share the features of Huawei deveco studio
Matlab -- CVX optimization kit installation
Redis分布式锁,没它真不行
Sword finger offer | number of 1 in binary
Window system black window redis error 20creating server TCP listening socket *: 6379: listen: unknown error19-07-28
Leetcode - 641 design cycle double ended queue (Design)*
Wavelet transform --dwt2 and wavedec2
Implementation of recommendation system collaborative filtering in spark
How to solve cross domain problems
百奥赛图与LiberoThera共同开发全人GPCR抗体药物取得里程碑式进展