当前位置:网站首页>Leetcode:528. select randomly according to the weight [ordinary random failure + prefix and dichotomy]
Leetcode:528. select randomly according to the weight [ordinary random failure + prefix and dichotomy]
2022-07-25 16:03:00 【White speed Dragon King's review】

analysis
Prefix and record the weight ratio of each
Then divide a random number in half , See which range it falls in , Which number does it belong to
Ordinary random
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()
Used several random , The accuracy of multiple mixing of pseudo-random numbers is lost
Prefix and dichotomy Only one random
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()
summary
Choose randomly by weight = The prefix and + Two points to see which area
边栏推荐
- Why is preparestatement better and safer?
- 共享锁(Shared Lock)
- mysql 隔离级别事务
- Geogle colab notes 1-- run the.Py file on the cloud hard disk of Geogle
- Matlab simulation of BPSK modulation system (1)
- Mysql读写锁
- 泰山OFFICE技术讲座:英寸,厘米,磅,派卡,提,行,字行,像素的换算关系
- MySQL tutorial 68-as setting alias
- How matlab saves all the data after running
- pymongo保存dataframe格式的数据(insert_one, insert_many, 多线程保存)
猜你喜欢

Baseband simulation system experiment of 4pam in Gaussian channel and Rayleigh channel

HDD Hangzhou station · harmonyos technical experts share the features of Huawei deveco studio

Gary marcus: learning a language is more difficult than you think

一文入门Redis

leetcode:154. 寻找旋转排序数组中的最小值 II【关于旋转排序数组的中后定位二分法】

产品动态丨Android 13 高效适配全新升级

【服务器数据恢复】HP EVA服务器存储意外断电导致RAID信息丢失的数据恢复案例

开发者如何为React Native选择合适的数据库

Leetcode - 232 realize queue with stack (design double stack to realize queue)

Pytoch learning notes - Teacher Liu Er RNN advanced chapter - code comments and results
随机推荐
MATLAB optimization tool manopt installation
Implementation of recommendation system collaborative filtering in spark
Leetcode - 303 area and retrieval - array immutable (design prefix and array)
MySQL教程68-AS 设置别名
Endnote cannot edit range resolution
MySQL 元数据锁(MDL)
Leetcode - 622 design cycle queue (Design)
# JWT 图解
"Digital security" alert NFT's seven Scams
PageHelper.startPage没有生效问题
MySQL-自增锁
2022-07-25日报:微软提出CodeT:代码生成新SOTA,20个点的性能提升
泰雷兹推出解决方案,助力SAP客户控制云端数据
HDD杭州站·HarmonyOS技术专家分享HUAWEI DevEco Studio特色功能
MySQL乐观锁
How to solve cross domain problems
Endnote add Chinese gbt7714 style how to quote documents in word
Pytoch learning notes -- seresnet50 construction
The difference between VaR, let and Const
Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式