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 ...
随机推荐
- 日记整理---->2016-11-01
这里我们整理一下项目的流程,一般来说做一个模块之前.会有需求文档.页面原型和接口文档. 一. js获取radio的值 页面的html代码: <ul class="list-group& ...
- 封装JDBC工具类
JDBC连接数据库基本的步骤是固定的,这样就可以考虑封装一个工具类来简化数据库操作. 封装时用到了Java中的properties配置文件,是以一种键值对的形式存在的,可以把连接数据库要动态的信息保存 ...
- 二、微信小游戏开发 多线程Worker
微信多线程Worker教程 微信多线程Worker API 一.创建Worker,并和当前线程通讯 多线程worker只能创建1个.能和当前线程互传数据. 创建worker 在微信开发者工具中,在当前 ...
- js中的颜色对应的常量代码code
颜色的对照表 颜色 英文代码 形像颜色 HEX格式 RGB格式 LightPink 浅粉红 #FFB6C1 255,182,193 Pink 粉红 #FFC0CB 255,192,203 ...
- Nexus介绍
转自:https://www.cnblogs.com/wincai/p/5599282.html 开始在使用Maven时,总是会听到nexus这个词,一会儿maven,一会儿nexus,当时很是困惑, ...
- Linux系统下 Apache+PHP 环境安装搭建
一.安装Apache2.2.221.到官网下载 http://httpd.apache.org/download.cgi ,选择相应的版本 可以先下载到windows系统中,上传到linux, 也可 ...
- SpringBoot SpringApplication底层源码分析与自动装配
目录 抛出问题 @SpringBootApplication注解剖析 SpringApplication类剖析 第一步:配置SpringBoot Bean来源 第二步 :自动推断SpringBoot的 ...
- 沈阳网络赛G-Spare Tire【容斥】
17.64% 1000ms 131072K A sequence of integer \lbrace a_n \rbrace{an} can be expressed as: \display ...
- JavaScript callback function 理解
看到segmentfault上的这个问题 JavaScript 回调函数怎么理解,觉得大家把异步和回调的概念混淆在一起了.做了回答: 我觉得大家有点把回调(callback)和异步(asynchron ...
- Python开发【Django】:中间件、CSRF
CSRF 1.概述 CSRF(Cross Site Request Forgery)跨站点伪造请求,举例来讲,某个恶意的网站上有一个指向你的网站的链接,如果某个用户已经登录到你的网站上了,那么当这个用 ...