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. php回顾(3)系统函数

    abs()         绝对值 ceil()         向上取整 floor()       向下取整 round()     四舍五入           第二个参数:保留小数点后面几位 ...

  2. .Net面向对象(OOP)

    序言 virtual虚方法 virtual 关键字用于在基类中修饰方法.virtual的使用会有两种情况: 情况1:在基类中定义了virtual方法,但在派生类中没有重写该虚方法.那么在对派生类实例的 ...

  3. 关于java代理(静态代理和动态代理)

    参考文章:http://kuangbaoxu.iteye.com/blog/193240

  4. mac重启nginx时报nginx.pid不存在的解决办法

    在安装nginx后,重启时发现报 nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such f ...

  5. C# WebServices 客户端服务端

    一.编写一个WebService 开发环境:VS2012 1.编写webservice阶段 打开VS2012,新建一个空的web应用程序,我这里用的Framework版本是4.5的 新建好web应用程 ...

  6. C++入门经典-例9.1-函数模板,函数模板的作用,使用数组作为模板参数

    1:函数模板不是一个实在的函数,因此编译器不能为其生成可执行的代码.定义函数模板只是一个对函数功能框架的描述,在具体执行时,将根据传递的实际参数决定其功能. 2:函数模板定义的一般形式如下: temp ...

  7. light4j轻量级微服务应用

    最近对light-4j轻框架比较感兴趣,于是对现有应用做了一次重构,现将其间的一些点滴所得分享出来. 项目打包 pom.xml配置了两个profile:debug支持mvn exec:exec启动应用 ...

  8. 黑马vue---8-10、v-cloak、v-text、v-html、v-bind、v-on的基本使用

    黑马vue---8-10.v-cloak.v-text.v-html.v-bind.v-on的基本使用 一.总结 一句话总结: v-bind等这些东西都是用的vue.data里面的变量 1.使用 v- ...

  9. HearthstoneBot

    https://github.com/ChuckFork/HearthstoneBot Sigmund Card game automation framework Hooks game and lo ...

  10. [windows菜鸟]C#中调用Windows API参考工具

    很多windows API都不知道签名,可以从下面几种方式进行查询 1.微软出的工具 P/Invoke Interop Assistant version 1.0 2.网站 pinvoke.net 3 ...