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 ...
随机推荐
- maven的概念-01
1.maven 简介 maven是Apach软件基金会维护的一款自动化构建工具: 作用是服务于java平台的项目构建和依赖管理: 2.关于项目构建 1)java代码 Java是一门编译型语言,.j ...
- P4981 父子 Cayley公式
CayleyCayley公式的定义是这样的,对于n个不同的节点,能够组成的无根树(原来是无向连通图或者是有标志节点的树)的种数是n^(n-2)种.(这里让大家好理解一点,就写成了无根树,其实应该是一样 ...
- HZOJ 20190722 visit (组合数学+数论)
考试T2,考试时打了个$O(n^3)$dp暴力,思路还是很好想的,但细节也不少,然后滚动数组没清空,而且题又看错了,只得了10pts,真是血的教训. 题解: 其实看数据范围,给出了模数是否为质数,其实 ...
- TensorFlow使用记录 (六): 优化器
0. tf.train.Optimizer tensorflow 里提供了丰富的优化器,这些优化器都继承与 Optimizer 这个类.class Optimizer 有一些方法,这里简单介绍下: 0 ...
- 也谈Tcp/Ip协议
一. 计算机网络体系结构分层 一图看完本文 计算机网络体系结构分层 计算机网络体系结构分层 不难看出,TCP/IP 与 OSI 在分层模块上稍有区别.OSI 参考模型注重“通信协议必要的功能是什么”, ...
- Suitable Replacement
D. Suitable Replacement 这个题统计出 s 和 t 中的各字母个数以及"?"的个数,直接暴力即可,s中不足的字母可用 "?"来替代 这个题 ...
- [题解] [CF451E] Devu and Flowers
题面 题解 就是一个求\(\sum_{i= 1}^{n}x _ i = m\)的不重复多重集的个数, 我们可以由容斥原理得到: \[ ans = C_{n + m - 1}^{n - 1} - \su ...
- CLOB、BLOB , CLOB与BLOB的区别
CLOB 定义 数据库中的一种保存文件所使用的类型. Character Large Object SQL 类型 CLOB 在 JavaTM 编程语言中的映射关系.SQL CLOB 是内置类型,它将字 ...
- JDK动态代理、CGLIB动态代理详解
Spring的AOP其就是通过动态代理的机制实现的,所以理解动态代理尤其重要. 动态代理比静态代理的好处: 1.一个动态代理类可以实现多个业务接口.静态代理的一个代理类只能对一个业务接口的实现类进行包 ...
- IDEA下载安装及绿色方法
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...