当前位置:网站首页>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
边栏推荐
- MySQL隐式锁
- CircleIndicator组件,使指示器风格更加多样化
- 权限管理-删除菜单(递归)
- Endnote cannot edit range resolution
- Product upgrade observation station in June
- 【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源
- Dpdk packet receiving and sending problem case: non packet receiving problem location triggered by mismatched packet sending and receiving function
- 不愧是阿里内部“千亿级并发系统架构设计笔记”面面俱到,太全了
- Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
- pymongo保存dataframe格式的数据(insert_one, insert_many, 多线程保存)
猜你喜欢

Leetcode - 379 telephone directory management system (Design)

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

用GaussDB(for Redis)存画像,推荐业务轻松降本60%

十字链表的存储结构

LeetCode - 641 设计循环双端队列(设计)*

HDD杭州站·HarmonyOS技术专家分享HUAWEI DevEco Studio特色功能

MATLAB optimization tool manopt installation

Reasons for data format conversion when matlab reads the displayed image

Leetcode - 380 o (1) time to insert, delete and get random elements (design hash table + array)

Redis distributed lock, it's really impossible without it
随机推荐
Redis分布式锁,没它真不行
Basic usage of MFC thread afxbeginthread, passing multiple parameters
【服务器数据恢复】HP EVA服务器存储意外断电导致RAID信息丢失的数据恢复案例
Simple rotation map and hamster beating
Copy a word style template to another document
递归菜单查询(递归:自己查自己)
Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
「数字安全」警惕 NFT的七大骗局
Beyond compare 4 realizes class file comparison [latest]
Baseband simulation system experiment of 4pam in Gaussian channel and Rayleigh channel
ServletConfig 类和ServletContext 类
Leetcode - 225 implements stack with queue
Typescript learning 1 - data types
# JWT 图解
LeetCode - 641 设计循环双端队列(设计)*
MySQL隐式锁
共2600页!又一份神级的面试手册面世~
CVPR 2022 | in depth study of batch normalized estimation offset in network
MySQL tutorial 66 data table query statement
一文入门Redis