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.
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的更多相关文章
- [LeetCode] Random Pick with Weight 根据权重随机取点
Given an array w of positive integers, where w[i] describes the weight of index i, write a function ...
- LeetCode 528. Random Pick with Weight
原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...
- [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 ...
- 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 ...
- 528. Random Pick with Weight
1. 问题 给定一个权重数组w,w[i]表示下标i的权重,根据权重从数组中随机抽取下标. 2. 思路 这道题相当于 497. Random Point in Non-overlapping Recta ...
- 【LeetCode】528. Random Pick with Weight 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-pi ...
- [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 ...
- [LeetCode] Random Pick with Blacklist 带黑名单的随机选取
Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...
- 710. Random Pick with Blacklist - LeetCode
Question 710. Random Pick with Blacklist Solution 题目大意:给一个N,表示一个范围[0,N),给一个黑名单列表blacklist,其中blacklis ...
随机推荐
- 《剑指offer》算法题第十天
今日题目: 数组中的逆序对 两个链表的第一个公共节点 数字在排序数组中出现的次数 二叉搜索树的第k大节点 字符流中第一个不重复的字符 1. 数组中的逆序对 题目描述: 在数组中的两个数字,如果前面一个 ...
- JVM(二),Java怎样实现一次编译到处运行(平台无关性)
二.Java怎样实现一次编译到处运行(平台无关性) 1.java平台无关性原理 Java源码首先被编译成字节码,再由不同平台的JVM进行解析,JAVA语言在不同的平台上运行时不需要进行重新编译,Jav ...
- codevs 2010 求后序遍历x
题目描述 Description 输入一棵二叉树的先序和中序遍历序列,输出其后序遍历序列. 输入描述 Input Description 共两行,第一行一个字符串,表示树的先序遍历,第二行一个字符串, ...
- zabbix服务端接收的数据类型,便于编写脚本向服务端提交数据
1.数据类型1:zabbix_agent执行脚本提交字典 UserParameter=tcp_port_listen,/usr/local/zabbix/share/script/get_game_p ...
- TensorFlow使用记录 (五): 激活函数和初始化方式
In general ELU > leaky ReLU(and its variants) > ReLU > tanh > logistic. If you care a lo ...
- 树状数组(BIT)
树状数组 树状数组是在线段树的结构上改造而来数据结构,主要用于完成: 给定一个初始值全为0的数列 ①给定i,计算返回a1+a2+--+ai的值 ②给定i和x,执行ai+=x BIT的求和 ll sum ...
- python-pandas-1
series Series 是pandas两大数据结构中(DataFrame,Series)的一种. 创建Series Series的定义:Series是一种类似于一维数组的对象,它由一组数据(各种N ...
- 库&插件&框架&工具
nodejs 入门 nodejs 入门教程,大家可以在 github 上提交错误 2016 年最好用的表单验证库 SMValidator.js 前端表单验证工具分享 浅谈前端线上部署与运维 说到前端部 ...
- ubuntu下apt-get 命令参数
转载:https://blog.csdn.net/linuxzhouying/article/details/7192612 ubuntu下apt-get 命令参数 常用的APT命令参数 apt-ca ...
- 再谈用Excel计算年龄
有的时候,对于客人的信息并不是全知,那么身份证就可能用15位来代替,这个时候怎么计算年龄呢?有一个很简单的公式,可以一次性计算15位或18位身份证的年龄. 首先,需要判断一下,这个身份证是15位还是1 ...