当前位置:网站首页>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()
总结
按权重随机选择 = 前缀和 + 二分看看落在哪个区域
边栏推荐
- Leetcode - 225 implements stack with queue
- LeetCode - 707 设计链表 (设计)
- Circulaindicator component, which makes the indicator style more diversified
- Wechat applet
- CircleIndicator组件,使指示器风格更加多样化
- Window system black window redis error 20creating server TCP listening socket *: 6379: listen: unknown error19-07-28
- 记得那两句话
- Use cpolar to build a business website (how to buy a domain name)
- Where is there a demo to set up the flex CDC to draw the number of MySQL?
- Data system partition design - partition and secondary index
猜你喜欢

Leetcode - 641 design cycle double ended queue (Design)*

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

Geogle colab notes 1-- run the.Py file on the cloud hard disk of Geogle

推荐收藏,这或许是最全的类别型特征的编码方法总结

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

Activity review | July 6 Anyuan AI X machine heart series lecture No. 2 | MIT professor Max tegmark shares "symbiotic evolution of human and AI"
![[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training](/img/d4/bcc577f320a893c7006177993b2e7a.png)
[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training

Why is preparestatement better and safer?

ML - Speech - traditional speech model

用GaussDB(for Redis)存画像,推荐业务轻松降本60%
随机推荐
行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
Pytoch learning notes -- Summary of common functions 2
微信小程序
Record Locks(记录锁)
LeetCode - 359 日志速率限制器 (设计)
LeetCode - 232 用栈实现队列 (设计 双栈实现队列)
IDEA—点击文件代码与目录自动同步对应
How Google cloud disk is associated with Google colab
LeetCode - 707 设计链表 (设计)
CVPR 2022 | in depth study of batch normalized estimation offset in network
Leetcode - 362 knock counter (Design)
MySQL隐式锁
Basic usage of MFC thread afxbeginthread, passing multiple parameters
Pytoch learning notes -- seresnet50 construction
TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format
不愧是阿里内部“千亿级并发系统架构设计笔记”面面俱到,太全了
Mysql读写锁
LeetCode - 641 设计循环双端队列(设计)*
LeetCode - 677 键值映射(设计)*
MySQL - Summary of common SQL statements