【LeetCode】528. Random Pick with Weight 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/random-pick-with-weight/description/
题目描述:
Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight.
Note:
1 <= w.length <= 10000
1 <= w[i] <= 10^5
pickIndex
will be called at most10000
times.
Example 1:
Input:
["Solution","pickIndex"]
[[[1]],[]]
Output: [null,0]
Example 2:
Input:
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
Output: [null,0,1,1,1,0]
Explanation of Input Syntax:
The input is two lists: the subroutines called and their arguments. Solution’s constructor has one argument, the array w. pickIndex has no arguments. Arguments are always wrapped with a list, even if there aren’t any.
题目大意
这个题目不太好理解,是要求按照权重挑选索引。比如[1,99]中,有1%的概率挑选到索引0,有99%的概率挑选到索引1.
解题方法
这个题很巧妙,我是想不出来的。做法是把概率分布函数转化为累计概率分布函数。然后通过随机数,进行二分查找。
比如,输入是 [1,2,3,4]
,那么概率分布是 [1/10, 2/10, 3/10, 4/10]
,累积概率分布是[1/10, 3/10, 6/10, 10/10]
. 总和是 10。如果我们产生一个随机数,在 1~10 之中,然后判断这个数字在哪个区间中就能得到对应的索引。
对于输入 [1,2,3,4]
,计算出来的 preSum 是 [1,3,6,10]
,然后随机选一个 s
,然后查找 s
属于哪个区间,各区间的含义是:
区间: [1], [2, 3], [4, 5, 6], [7, 8, 9, 10]
preSum: 1, 3, 6, 10
返回值: 1, 2, 3, 4
相当于找比 s 大的 preSum 值的索引。
如果还不理解,那么就想一想这个 preSum 的间隔,是不是发现这个间隔对应了题目的输入?那么选随机数找 upper_bound 的时候那就不是把一个区间里的数字合并到了某个 preSum 值上,而且 preSum 是不是对应着输入?所以是不是就把这个某个区间内的随机数对应上了一个输入值?
总之,随机的数字在哪个区间当中,那么就返回这个区间对应的数字即可。
这个二分查找也可以好好学习一下。
代码如下:
class Solution:def __init__(self, w):""":type w: List[int]"""self.preSum = [0] * len(w)self.preSum[0] = w[0]for i in range(1, len(w)):self.preSum[i] = self.preSum[i - 1] + w[i]def pickIndex(self):""":rtype: int"""total = self.preSum[-1]rand = random.randint(0, total - 1)left, right = 0, len(self.preSum) - 1while left + 1 < right:mid = (left + right) // 2if rand >= self.preSum[mid]:left = midelse:right = midif rand < self.preSum[left]:return leftreturn right# Your Solution object will be instantiated and called as such:
# obj = Solution(w)
# param_1 = obj.pickIndex()
日期
2018 年 8 月 18 日 —— 天在下雨
【LeetCode】528. Random Pick with Weight 解题报告(Python)相关推荐
- leetcode 528. Random Pick with Weight
给一个权重的vector,让你根据权重的概率返回值,返回的值是这些权重的索引. 比如给你一个[1,2]的权重矩阵,1/3的概率返回0,2/3的概率返回1. 等概率函数random只能等概率的一系列数, ...
- LeetCode第45场双周赛-解题报告
LeetCode第45场双周赛-解题报告 A. 唯一元素的和 原题链接 https://leetcode-cn.com/problems/sum-of-unique-elements/ 解题思路 因为 ...
- 【LeetCode】436. Find Right Interval 解题报告(Python)
[LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- leetcode 398. Random Pick Index | 398. 随机数索引(Java)
题目 https://leetcode.com/problems/random-pick-index/ 题解 常规思路,先用 map 存一串,取的时候从 map 对应的串中随机拿一个就 ok. cla ...
- python leetcode 398. Random Pick Index
奇怪的是蓄水池抽样算法无法AC 代码2是蓄水池抽样 class Solution(object):import randomdef __init__(self, nums):""& ...
- leetcode 214. 最短回文串 解题报告
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...
- 【LeetCode】517. 超级洗衣机 解题报告 (python)
原题地址:https://leetcode-cn.com/problems/super-washing-machines/submissions/ 题目描述: 假设有 n 台超级洗衣机放在同一排上.开 ...
- Leetcode 1190. Reverse Substrings Between Each Pair of Parentheses解题报告(python)
1190. Reverse Substrings Between Each Pair of Parentheses Reverse Substrings Between Each Pair of Pa ...
- [leetcode] 273. Integer to English Words 解题报告
题目链接:https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its e ...
最新文章
- Python sorted() 函数
- python templates_详解Python的Django框架中的templates设置
- Tensorflow-gpu安装
- python 加锁_python之给文件加锁(fcntl模块)
- linux创建备用管理员,sql server 创建备用管理员和只读用户
- 计算机接口教程,运用接口实现计算机各组件信息
- “编程能力差,90%输在了数学上!”CTO:多数程序员都是瞎努力!
- linux打开python3_linux 进入python
- ORACLE字符集基础知识
- FFmpeg入门测试
- Vulkan入门(9)-渲染和显示.md
- dht11温湿度传感器工作原理引脚功能电路接线图
- 轻松实现钉钉机器人定时发消息
- 计算机ms高级应用科目一 科目二考什么,科目一、科目二、科目三、科目四都考什么?全都在这儿了!...
- 入门月薪8k,3年年薪35w,大数据的就业前景与薪酬待遇浅析
- firefox火狐浏览器flash播放视频
- 走进科学之内存插槽插满不识别
- 【C/C++】ifndef/define/endif的作用
- 第一次有人把5G讲得这么简单明了!
- 免费的外文文献搜索下载工具推荐
热门文章
- 赚钱软件应该怎么选择?你应该知道的副业兼职赚钱软件
- 180g6服务器支持显卡超频吗,显卡怎么超频?五种显卡超频方法随你挑!
- .dcm格式文件软件读取及python处理
- 好用的老照片修复神器软件有哪些?这三个工具帮你留住回忆
- 有害气体在线监测数采仪
- 文档管理系统 LogicalDOC
- 基于SSM的网上餐厅管理系统
- 利用 jQuery 操作页面元素的方法,实现电商网站购物车页面商品数量的增加和减少操作,要求单项价格和总价随着数量的改变而改变
- 《嵌入式系统原理与接口技术》第1章 绪论1.1 嵌入式系统概述
- 找出你的高价值潜在用户 - 通过归因分析实现用户画像和精准营销