当前位置:网站首页>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
边栏推荐
- The difference between VaR, let and Const
- 共享锁(Shared Lock)
- Introduction to redis
- Typescript learning 1 - data types
- Leetcode - 379 telephone directory management system (Design)
- Matlab -- CVX optimization kit installation
- 狂神redis笔记12
- Leetcode - 380 o (1) time to insert, delete and get random elements (design hash table + array)
- Geogle colab notes 1-- run the.Py file on the cloud hard disk of Geogle
- 基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
猜你喜欢

Leetcode - 707 design linked list (Design)

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

Introduction to redis

Pytoch learning notes -- seresnet50 construction

Leetcode - 622 design cycle queue (Design)

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

活动回顾|7月6日安远AI x 机器之心系列讲座第2期|麻省理工教授Max Tegmark分享「人类与AI的共生演化 」

Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output

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

LeetCode - 359 日志速率限制器 (设计)
随机推荐
Boomi荣获“多元化最佳首席执行官奖”和“职业成长最佳公司奖”,在大型公司类别中跻身50强
通用测试用例写作规范
Implementation of recommendation system collaborative filtering in spark
HDD Hangzhou station · harmonyos technical experts share the features of Huawei deveco studio
Leetcode - 225 implements stack with queue
R语言偏相关性计算(Partial Correlation)、使用ggm包的pcor函数计算偏相关性(Partial Correlations)
MySQL 悲观锁
I want to ask whether the variable configuration function can only be used in SQL mode
pymongo保存dataframe格式的数据(insert_one, insert_many, 多线程保存)
面试8家公司,1周拿了5个offer,分享一下自己的心得
Leetcode - 677 key value mapping (Design)*
Zhaoqi Kechuang high-level innovation and Entrepreneurship Talent Service Platform at home and abroad, mass entrepreneurship and innovation achievement transformation platform
MySQL tutorial 65 data in MySQL operation table
Product upgrade observation station in June
Leetcode - 622 design cycle queue (Design)
[Shakespeare: keep the fun of being a man]
mysql 隔离级别事务
BSC智能链合约模式系统开发详情
如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?
MySQL页锁