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 ...
随机推荐
- java生成webservice方法
参考: https://note.youdao.com/ynoteshare1/index.html?id=c10324bb3b794baece3d2ae9faadc5c1&type=note
- C++异常 返回错误码
一种比异常终止更灵活的方法是,使用函数的返回值来指出问题.例如,ostream类的get(void)成员ASCII码,但到达文件尾时,将返回特殊值EOF.对hmean()来说,这种方法不管用.任何树脂 ...
- 初学hadoop,windows下安装
先bb一下,woc开始使用Cygwin来模拟linux配置hadoop,然后各种错误,找着找着发现原来2.0+的hadoop可以直接在windows下配置.当时真是1w头神兽飞过. 下载hadoop ...
- 【BZOJ4282】慎二的随机数列 乱搞
[BZOJ4282]慎二的随机数列 Description 间桐慎二是间桐家著名的废柴,有一天,他在学校随机了一组随机数列, 准备使用他那强大的人工智能求出其最长上升子序列,但是天有不测风云,人有旦夕 ...
- ehcache加载配置文件ehcache.xml的源码
package net.sf.ehcache.config; public final class ConfigurationFactory { public static Configuration ...
- ThinkPHP的增删改查!
对表的操作: 增加:M('表名')->add($data); (可以是数组) 删除:M('表名')->delete($data); (不可以是数组,删除多个有另外的方法) 修改:M('表 ...
- python range函数与numpy arange函数
1.range()返回的是range object,而np.arange()返回的是numpy.ndarray() range尽可用于迭代,而np.arange作用远不止于此,它是一个序列,可被当做向 ...
- 记录用户操作历史命令history
我们知道可以使用history命令,查看自己的操作记录,但如果你是root用户,如何查看其它用户的操作记录呢? 其实history命令只是把当前用户目录下的~/.bash_History文件内容列 ...
- chinese-typesetting:更好的中文文案排版
欢迎指正.GitHub 地址:https://github.com/jxlwqq/chinese-typesetting 更好的中文文案排版 统一中文文案.排版的相关用法,降低团队成员之间的沟通成本, ...
- oracle权限赋予
上节讲的创建的software用户能否访问其他用户的表呢 1,创建software用户,密码设置为system create user software identified by system 2, ...