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 ...
随机推荐
- js里面正则表示满足多个条件的写法
实例,满足条件里面必须包含数字,字母和下划线组成 代码如下: var reg = /^([a-z]+\d+\_+)|([a-z]+\_+\d+)|(\_+[a-z]+\d+)|(\_+\d+[a-z] ...
- KVO的用法、底层实现原理
KVO的用法 KVO也就是key-value-observing(即键值观察),利用一个key来找到某个属性并监听其值得改变.用法如下: 添加观察者 在观察者中实现监听方法,observeValueF ...
- 原生JS,实现图片可拖拽,并且移动四个角和四条变能够自由变换图片大小
网上搜的资料,源码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- LNMP redis 安装、PHPredis扩展配置、服务器自启动、redis认证密码
背景: LNMP 环境(centos7) 一. 安装redis 1.下载,解压,编译 $ cd /usr/local$ wget http://download.redis.io/releases/r ...
- 禁止同一条ajax请求重复发出的方法
在项目中,遇到的问题是: 四个tab发送四个不同的请求,当用户连续在不同的按钮之间来回切换时,会出现不清楚那条数据是需要展示的的问题,和当连续点击同一个按钮时,基本同时返回的数据会全都展示出来的问题. ...
- 【Android 7.1.1】 锁屏界面点击“空白处”响应事件
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLa ...
- linux下一些常用命令和访问目录
1. 目录 ls 列出目录文件名 ll 列出所有目录文件的访问权限等相关信息,包括 . .. ls -a 列出所有目录文件名,包括 . .. ls - ...
- WEB安全第七篇--终结篇考验逻辑思维:逻辑漏洞大汇总(越权、会话逻辑、业务逻辑、暴力破解)
零.前言 最近做专心web安全有一段时间了,但是目测后面的活会有些复杂,涉及到更多的中间件.底层安全.漏洞研究与安全建设等越来越复杂的东东,所以在这里想写一个系列关于web安全基础以及一些讨巧的pay ...
- window自带字体
一.在默认情况下, Windows 默认提供下列字体: Windows 95/98/98SE 宋体.黑体.楷体_GB2312.仿宋_GB2312 Windows XP/2000/2003/ME/NT ...
- windows环境下最简单的nginx + tomcat负载均衡配置示例
后端是两台tomcat服务器,我们简称为node1 和node2,访问地址分别是 http://192.168.1.2:8080 和 http://192.168.1.4:8080 前端使用nginx ...