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,后来就想着直 ...
随机推荐
- Python3.7 exe编译工具对比zz
For years, NVDA has used Py2exe to package Python code into something that is executable on a system ...
- SQL --------------- GROUP BY 函数
Aggregate 函数常常需要添加 GROUP BY 语句,Aggregate函数也就是常说的聚和函数,也叫集合函数 GROUP BY语句通常与集合函数(COUNT,MAX,MIN,SUM,AVG) ...
- Ubuntu 16 PPA源管理(查询、添加、修改、删除)
查询 在Ubuntu中,每个PPA源是单独存放在/etc/apt/sources.list.d/文件夹中的,进入到该文件夹,使用ls命令查询即可列出当前系统添加的PPA源. 添加 sudo add-a ...
- Java自学-类和对象 访问修饰符
Java的四种访问修饰符 成员变量有四种修饰符 private 私有的 package/friendly/default 不写 protected 受保护的 public 公共的 比如public 表 ...
- 2019年12月的第一个bug
现在是2019年12月1日0点27分,我的心情依旧难以平静.这个月是2019年的最后一个月,是21世纪10年代的最后一个月,也是第一批90后30岁以前的最后一个月.就是在这个月的第一天的0点0分,我写 ...
- Fuck SELinux :rsyslog无法生成log文件,原来是selinux机制搞的鬼!
Fuck SELinux 一万年! 关闭即可.
- 【java】查看Java字节码文件内容的方法+使用javap找不到类 解决方法
研究synchronized底层实现,涉及到查看java字节码的需要 前提是,你的PC已经成功安装了JDK并别配置了环境变量. ==========查看方法========= 一.javap查看简约字 ...
- 前端面试01:描述一下cookices sessionStorage 和 localStorage 的区别
相同点:都可以存储在客户端 不同点: 1.存储大小 cookie数据大小不能超过4K. sessionStorage 和 localStorage 虽然也有大小限制,但是比cookie大得多,可以达到 ...
- Fluentvalidation的基本使用
前言: fluentvalidation用于构建强类型验证规则的流行.NET库.方便好用快捷省心!!! 本文按照官方文档进行试验,如果深(不)入(看)的(我)研(写)究(的)请去官网:https:// ...
- Haskell路线
@ 知乎 @ <I wish i have learned haskell> ———— 包括: Ranks, forall, Monad/CPS, monadic parser, FFI ...