原题链接在这里: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. 【操作系统之十四】iptables扩展模块

    1.iprange 使用iprange扩展模块可以指定"一段连续的IP地址范围",用于匹配报文的源地址或者目标地址.--src-range:匹配报文的源地址所在范围--dst-ra ...

  2. 5行代码带你实现一个js的打字效果

    (转载)原文链接:https://juejin.im/post/5ddf55835188257313541581 前言 有次看电影

  3. vue-v-xxx基于 Vue拓展的 v-xxx 库

    君问归期未有期,巴山夜雨涨秋池. 何当共剪西窗烛,却话巴山夜雨时. 作为vue轻车熟路的老司机,经常会用到一些指令,vue官方提供的指令又太少,无法满足旺盛的欲望,而每次要写一遍,终日郁郁寡欢,从小就 ...

  4. Mysql中的变量

    Mysql中的变量众多(即运行的配置),如:事务相关的.连接相关的.查询优化类的等等. 变量的作用域: 1.临时作用域 session级别:即打开一个与mysql server会话的基础上的作用域,变 ...

  5. C#环境配置

    由于C#是,Net框架的一部分,且用于编写.Net应用程序,所以我们需先了解下C#与.Net框架之间的关系. .Net框架(.Net Framework) .Net框架是一个创新的平台,能帮你编写出下 ...

  6. (二)RFB协议具体通信说明

    .   消息说明  8.1     握手消息 8.1.1       RFB协议版本号 1.vnc服务器发送所能够支持的最高RFB协议版本号给客户端,格式如下: “RFB xxx.yyy\n”共12b ...

  7. 基于roslyn的动态编译库Natasha

    人老了,玩不转博客园的编辑器,详细信息转到:https://mp.weixin.qq.com/s/1r6YKBkyovQSMUgfm_VxBg 关键字:Github, NCC, Natasha,Ros ...

  8. Python——XPath提取某个标签下所有文本

    /text()获取指定标签下的文本内容,//text()获取指定标签下的文本内容,包括子标签下的文本内容,比较简单的是利用字符串相加: room_infos = li.xpath('.//a[@cla ...

  9. 4-rocketmq 发送时异常:system busy 和 broker busy 解决方案

    原文:https://www.cnblogs.com/enenen/p/10138511.html 推荐阅读:https://juejin.im/post/5d996285f265da5bad4052 ...

  10. 2019 中细软java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.中细软等公司offer,岗位是Java后端开发,因为发展原因最终选择去了中细软,入职一年时间了,也成为了面试官 ...