首页 技术 正文
技术 2022年11月15日
0 收藏 932 点赞 3,107 浏览 1943 个字

原题链接在这里: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. 1 <= w.length <= 10000
  2. 1 <= w[i] <= 10^5
  3. pickIndex will be called at most 10000 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 wpickIndex 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 IndexLinked List Random Node.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,984
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,839