528. Random Pick with Weight
1. 问题
给定一个权重数组w,w[i]表示下标i的权重,根据权重从数组中随机抽取下标。
2. 思路
这道题相当于 497. Random Point in Non-overlapping Rectangles的一个简化版。
(1)可以先依次对每个下标的权重累加存起来(相当于概率分布中的分布累积函数CDF,Cumulative Distribution Function)。
(2)从 [1, 总权重] 中随机取一个数n,根据这个数在多个累加权重之间寻找合适的位置,即可完成题目要求的随机采样。
(3)可以使用python的bisect_left方法,bisect_left的作用是对已排序数组,查找目标数值将会插入的位置并返回(但是不会插入),数值相同时返回靠左的位置。
init:时间复杂度O(n),空间复杂度O(n)
pickIndex:时间复杂度O(n),空间复杂度O(1)
3. 代码
import random
import bisect
class Solution(object):
def __init__(self, w):
"""
:type w: List[int]
"""
self.accumulate = []
sums = 0
for weight in w:
sums += weight
self.accumulate.append(sums)
def pickIndex(self):
"""
:rtype: int
"""
n = random.randint(1,self.accumulate[-1])
i = bisect.bisect_left(self.accumulate, n)
return i
# Your Solution object will be instantiated and called as such:
# obj = Solution(w)
# param_1 = obj.pickIndex()
4. 类似题目
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight的更多相关文章
- LeetCode 528. Random Pick with Weight
原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...
- 528. Random Pick with Weight index的随机发生器
[抄题]: Given an array w of positive integers, where w[i] describes the weight of index i, write a fun ...
- 【LeetCode】528. Random Pick with Weight 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-pi ...
- [leetcode]528. Random Pick with Weight按权重挑选索引
Given an array w of positive integers, where w[i] describes the weight of index i, write a function ...
- [LeetCode] Random Pick with Weight 根据权重随机取点
Given an array w of positive integers, where w[i] describes the weight of index i, write a function ...
- [Swift]LeetCode528. 按权重随机选择 | Random Pick with Weight
Given an array w of positive integers, where w[i] describes the weight of index i, write a function ...
- Random Pick with Weight
Given an array w of positive integers, where w[i] describes the weight of index i, write a function ...
- [LeetCode] Random Pick with Blacklist 带黑名单的随机选取
Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...
- 710. Random Pick with Blacklist - LeetCode
Question 710. Random Pick with Blacklist Solution 题目大意:给一个N,表示一个范围[0,N),给一个黑名单列表blacklist,其中blacklis ...
随机推荐
- ZooKeeper(四)-- 第三方客户端 ZkClient的使用
前言 zkClient主要做了两件事情: 一件是在session loss和session expire时自动创建新的ZooKeeper实例进行重连. 另一件是将一次性watcher包装为持久watc ...
- Winform的学习
昨天由于一些原因没有上课啦,虽然也看啦一些东西,但是感觉太少也就没有来啦,嘿嘿,今天认真地了解啦winform,学习了一些控件的使用,但是感觉好多属性知道怎么用的,却还是记得不太清楚,感觉看到啦知道, ...
- 为什么setinterval和settimeout越点击越快以及响应的解决办法
setinterval大家都很了解,但是如果时间长的话,误差也会越来越大,所以我习惯上使用settimeout的递归,闲来没事,写了一个定时器的递归 <!DOCTYPE html> < ...
- COM组件技术名称解释
GUID:全局唯一标识. CLSID 或 ProgID :唯一地表示一个组件服务程序,那么根据这些ID,就可以加载运行组件,并为客户端程序提供服务了. IID :唯一的表示接口ID. COM 组件是运 ...
- 心脏滴血HeartBleed漏洞研究及其POC
一.漏洞原理: 首先声明,我虽然能看懂C和C++的每一行代码,但是他们连在一起我就不知道什么鬼东西了.所以关于代码说理的部分只能参考其他大牛的博客了. /* 据说源码中有下面两条语句,反正我也没看过源 ...
- c# Socket通信基础
一.IP地址操作类 1.IPAddress类 a.在该类中有一个 Parse()方法,可以把点分的十进制IP表示转化成IPAddress类,方法如下: IPAddress address = I ...
- Tomcat 500错误 问题集锦
HTTP 500 - 内部服务器错误 1.jdk版本与Tomcat版本不一样. 问题: 配置一个Web应用的时候,源文件和server.xml.web.xml的配置都没有问题,但是在访问到一个Ser ...
- centos7 安装后,出现Please make your choice from above ['q' to quit | 'c' to continue | 'r' to refresh]
PS:出现以上信息,是要求你阅读或者接收协议: Initial setup of CentOS Linux 7 (core)解决步骤如下: 1,输入[1],按Enter键阅读许可协议,2,输入[2], ...
- js封装正则验证
//根据不同的验证内容,返回相应的正则表达式 function returnRegString(regName) { if (regName == "email") { retur ...
- ManyToMany参数(through,db_constraint)
through : 指定自己写好的第三张表,我们可以给第三张表添加字段了(告诉Django不用建第三张表了,我们都给他配好了) class Book(models.Model): name=model ...