原题链接在这里: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.

LeetCode 528. Random Pick with Weight的更多相关文章

  1. [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  ...

  2. 【LeetCode】528. Random Pick with Weight 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-pi ...

  3. 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 ...

  4. 528. Random Pick with Weight

    1. 问题 给定一个权重数组w,w[i]表示下标i的权重,根据权重从数组中随机抽取下标. 2. 思路 这道题相当于 497. Random Point in Non-overlapping Recta ...

  5. [LeetCode] Random Pick with Weight 根据权重随机取点

    Given an array w of positive integers, where w[i] describes the weight of index i, write a function  ...

  6. [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  ...

  7. [LeetCode] 398. Random Pick Index ☆☆☆

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  8. Random Pick with Weight

    Given an array w of positive integers, where w[i] describes the weight of index i, write a function  ...

  9. [leetcode] 398. Random Pick Index

    我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...

随机推荐

  1. 从图(Graph)到图卷积(Graph Convolution):漫谈图神经网络模型 (三)

    本文属于图神经网络的系列文章,文章目录如下: 从图(Graph)到图卷积(Graph Convolution):漫谈图神经网络模型 (一) 从图(Graph)到图卷积(Graph Convolutio ...

  2. 零次学习(Zero-Shot Learning)入门(转)

    很久没有更文章了,主要是没有找到zero-shot learning(ZSL)方面我特别想要分享的文章,且中间有一段时间在考虑要不要继续做这个题目,再加上我懒 (¬_¬),所以一直拖到了现在. 最近科 ...

  3. 记一次ssh.exec_command(cmd)执行后读取结果为空

    # 连接跳板机,执行插标签 def con_tmp_machine(mobile_phoneno, myguid): keyfile = os.path.expanduser('/Users/kusy ...

  4. gcc/g++ -O 优化选项说明

    查查gcc手册就知道了,每个编译选项都控制着不同的优化选项 下面从网络上copy过来的,真要用到这些还是推荐查阅手册 -O设置一共有五种:-O0.-O1.-O2.-O3和-Os. 除了-O0以外,每一 ...

  5. 阿里OSS前端直传

    第一次写博客,如有错误请多多指教. 先上代码吧: ossUpload = function (file, fun, funParameter) { //第一此请求后台服务器获取认证请求 $.ajax( ...

  6. JavaScaript学习笔记第(一)

    js由三部分组成,分别是ECMAScript.DOM.BOM 其中ECMAScript规定了js的语法 js是一门解释型语言.脚本语言.动态类型语言.基于对象语言 书写js代码和CSS一样,有三个书写 ...

  7. uni-app采坑记录

    1. uni-app采坑记录 1.1. 前言 这里记录下uni-app实践中踩的坑 1.2. 坑点 1.2.1. 触发事件@longTap和@longpress 这两个都表示长按触发事件,那么这两个有 ...

  8. Clang交叉编译初识

    最近工作中要编译一个第三方的C库用于iOS端使用,我直接在Mac OS的终端中./configure & make & make install常规走下来,却无法在真机iOS上使用,提 ...

  9. VUE--v-on修饰符

    1.v-on的修饰符 .stop:阻止事件冒泡 <div @click="getTitle"> 阿Q <button @click="getBut&qu ...

  10. Get SAP Code Page by External Name

    CL_ABAP_CODEPAGE - SAP_CODEPAGE 取SAP对应code page内码: