LeetCode 528. Random Pick with Weight
原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/
题目:
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 <= 100001 <= w[i] <= 10^5pickIndexwill be called at most10000times.
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.
题解:
The given input array means for current index i, the weight is w[i].
e.g. w = [20, 20, 60]. The weight of choosing 0 is 20, the weight of choosing 1 is 20, the weight of choosing 2 is 60, totally it is 100.
In order to choose by weight, we need to accumlat the total weight, and pick a random number within [1, total weight].
sum = [20, 40, 100]
And binary search where this weight would land.
If sum[mid] == pick, then simply return mid.
Else if sum[mid] < pick, say pick = 50, sum[mid] = 40, then it can't be index 1, since 1 is from 20 to 40. l = mid + 1.
Else if sum[mid] > pick, say pick = 30, sum[mid] = 40, then it could still be index 1, since 1 is from 20 to 40. r = mid.
When using above transion, while loop condision is l < r, it can't be l <= r. Otherwise, it would get out of loop.
Time Complexity: pickIndex, O(logw.length).
Space: O(1). It doesn't have extra array, it is changing input w array.
AC Java:
class Solution {
int [] sum;
Random rand;
public Solution(int[] w) {
for(int i = 1; i<w.length; i++){
w[i] += w[i-1];
}
this.sum = w;
this.rand = new Random();
}
public int pickIndex() {
int len = sum.length;
int n = sum[len-1];
int pick = rand.nextInt(n) + 1;
int l = 0;
int r = len-1;
while(l < r){
int mid = l + (r - l) / 2;
if(sum[mid] == pick){
return mid;
}else if(sum[mid] < pick){
l = mid + 1;
}else{
r = mid;
}
}
return l;
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(w);
* int param_1 = obj.pickIndex();
*/
类似Random Pick Index, Linked List Random Node.
LeetCode 528. Random Pick with Weight的更多相关文章
- [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】528. Random Pick with Weight 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-pi ...
- 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 ...
- 528. Random Pick with Weight
1. 问题 给定一个权重数组w,w[i]表示下标i的权重,根据权重从数组中随机抽取下标. 2. 思路 这道题相当于 497. Random Point in Non-overlapping Recta ...
- [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 ...
- [LeetCode] 398. Random Pick Index ☆☆☆
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- Random Pick with Weight
Given an array w of positive integers, where w[i] describes the weight of index i, write a function ...
- [leetcode] 398. Random Pick Index
我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...
随机推荐
- CF 715 E. Complete the Permutations
CF 715 E. Complete the Permutations 题目大意:给定两个排列\(p,q\)的一部分.定义两个排列\(p,q\)的距离为使用最少的交换次数使得\(p_i=q_i\).对 ...
- Oracle 自定义函数实现列转行效果
在 Oracle 领域,我相信一说到列转行大部分人都会立马想到 WM_CONCAT 函数,我觉得主要是因为该函数比较实用.但事实上 WM_CONCAT 并非官方公开函数,使用会存在一定的风险:函数返回 ...
- PostgreSQL事务特性之嵌套事务
嵌套事务的实现是基于SAVEPOINT.ROLLBACK TO SAVEPOINT和RELEASE SAVEPOINT的,也就是设置一个保存点,可以回滚到保存点和释放保存点. 测试表的初始状态如下: ...
- Python爬虫实战之爬取糗事百科段子【华为云技术分享】
首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把,这次我们尝试一下用爬虫把他们抓取下来. 友情提示 糗事百科在前一段时间进行了改版,导致之前的代码没法用了,会导致无法输出和CPU占用过高的 ...
- yml 字典列表
观察: --- # 一位职工记录 name: Example Developer job: Developer skill: Elite employed: True foods: - Apple - ...
- 阿里云 OSS 如何设置防盗链, 上个月图床流量耗费50G+,请求次数10W+,什么鬼?
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- 使用Docker之容器的创建、删除
假设我们现在拥有以下镜像 1:启动一个简单容器 docker container run //用于启动一个容器 -it //是容器具有交互性,并与终端进行连接 命令:docker c ...
- windows 10 配置Java 环境变量(5步骤)
前提:1.windows 10 系统(不是win8,也不是win7)2.安装JDK步骤 1. 打开 环境变量窗口右键 This PC(此电脑) -> Properties(属性) -> A ...
- redis集群cluster简单设置
环境: 这里参考官方使用一台服务器:Centos 7 redis-5.0.4 192.168.10.10 redis集群cluster最少要3个主节点,所以本次需要创建6个实例:3个主节点,3 ...
- mac下搭建Apache服务器环境
mac下自带了一个Apache服务环境,所以不需要另外去下载,直接配置就好了. 一.启动Apache服务 在终端下输入 sudo apachectl start , 启动Apache服务.在浏览器输入 ...