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.

 class Solution {
Random random;
int[] wSums; public Solution(int[] w) {
this.random = new Random();
for (int i = ; i < w.length; ++i) {
w[i] += w[i - ];
}
this.wSums = w;
} public int pickIndex() {
int len = wSums.length;
int idx = random.nextInt(wSums[len - ]) + ;
int left = , right = len - ;
while (left <= right) {
int mid = left + (right - left) / ;
if (wSums[mid] == idx) {
return mid;
} else if (wSums[mid] < idx) {
left = mid + ;
} else {
right = mid - ;
}
}
return left;
}
}

Random Pick with Weight的更多相关文章

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

    原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...

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

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

  5. 528. Random Pick with Weight

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

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

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

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

  8. [LeetCode] Random Pick with Blacklist 带黑名单的随机选取

    Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...

  9. 710. Random Pick with Blacklist - LeetCode

    Question 710. Random Pick with Blacklist Solution 题目大意:给一个N,表示一个范围[0,N),给一个黑名单列表blacklist,其中blacklis ...

随机推荐

  1. 《剑指offer》算法题第九天

    今日题目: 整数中1出现的次数 把数组排成最小的数 丑数 第一个只出现一次的字符位置 今天的题目相对比较难,特别是第1题和第3题很考验数学功底,下面我们一题一题来看看. 1.整数中1出现的次数 题目描 ...

  2. P2015 二叉苹果树,树形dp

    P2015 二叉苹果树 题目大意:有一棵二叉树性质的苹果树,每一根树枝上都有着一些苹果,现在要去掉一些树枝,只留下q根树枝,要求保留最多的苹果数(去掉树枝后不一定是二叉树) 思路:一开始就很直接的想到 ...

  3. HGOI 20191029am 题解

    Promblem A 小G的字符串 给定$n,k$,构造一个长度为$n$,只能使用$k$种小写字母的字符串. 要求相邻字符不能相同且$k$种字母都要出现 输出字典序最小的字符串,无解输出$-1$. 对 ...

  4. Codeforces 1214 F G H 补题记录

    翻开以前打的 #583,水平不够场上只过了五题.最近来补一下题,来记录我sb的调试过程. 估计我这个水平现场也过不了,因为前面的题已经zz调了好久-- F:就是给你环上一些点,两两配对求距离最小值. ...

  5. JSTL的forEach标签中的属性具体含义

    JSTL的forEach标签在JSP页面经常替代Java脚本的循环语句,生成多个记录的信息.一般只需 一个一个的展示记录即可,有些需要获取当前记录的索引.在需要获取当前记录的索引的时候可能 有点麻烦, ...

  6. Linux Shell脚本,删除旧文件,保留最新的几个文件

    删除某一目录下文件,只保留最新的几个 #!/bin/bash #保留文件数 ReservedNum= FileDir=/home/dev/saas_test/testcases/report/html ...

  7. Inter IPP & Opencv 在centos 环境下使用GCC命令行编译c++运行

    Inter IPP & Opencv 的安装看这里:https://www.cnblogs.com/dzzy/p/11332907.html 考虑到服务器一般没有桌面环境,不能用IDE编译,直 ...

  8. 在CentOS 7上搭建WordPress

    环境(ECS阿里云服务器) 服务器操作系统:CentOS 7.3 : 博客部署服务器:Apache HTTP: 数据库:MySql: 框架:WordPress: 步骤 一.安装 Apache HTTP ...

  9. sql把一段时间分割成周,月,季度,年的时间段

    --本周 select TO_CHAR(CREATE_DATE ,'yyyy-MM-dd')as NEW_DATE , TO_CHAR(trunc(CREATE_DATE, ,'yyyy-MM-dd' ...

  10. springboot 热部署替代方式

    因为使用的 idea springboot2.2.0 snapshot版本, 常规的devtools方法实在是实现不了热部署,所以采用手动update的方法更新,测试可以成功更新resource里面的 ...