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

s

原文地址:https://www.cnblogs.com/grandyang/p/9784690.html

时间: 2024-10-10 03:09:23

[LeetCode] Random Pick with Weight 根据权重随机取点的相关文章

select random item with weight 根据权重随机选出

http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/ 类似俄罗斯轮盘赌 select random item with weight 根据权重随机选出,布布扣,bubuko.com

[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 pickIndex which randomly picks an index in proportion to its weight. Note: 1 <= w.length <= 10000 1 <= w[i] <= 10^5 pickIndex will be called at

LeetCode 528. Random Pick with Weight / 497. Random Point in Non-overlapping Rectangles

528. Random Pick with Weight 根据weight随机选取一个数,用 Prefix Sum+Binary Search 来解决. https://www.geeksforgeeks.org/random-number-generator-in-arbitrary-probability-distribution-fashion/ class Solution { public: vector<int> prefixSum; int total=0; Solution(v

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

Given a blacklist B containing unique integers from [0, N), write a function to return a uniform random integer from [0, N) which is NOT in B. Optimize it such that it minimizes the call to system’s Math.random(). Note: 1 <= N <= 1000000000 0 <=

Leetcode: Random Pick Index

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Note: The array size can be very large. Solution that uses too much extra sp

根据权重随机选取指定条数记录的简单算法实现(C#)

一.应用场景: 有时我们需要从一些列数据中根据权重随机选取指定条数记录出来,这里需要权重.随机,我们根据权重越大的,出现概率越大.例如广告系统:可根据客户支付金额大小来调控客户们的广告出现概率,客户支付金额越大,其广告出现频率越频繁,例如:加入有10条广告,然后每条广告都有一个权重,我们每次要根据权重选取5条广告出来进行显示.有了需求,我们就进行解决,本文章就是利用一种简单的算法来实现根据权重来随机选取. 二.简单算法的实现: 根据我们需求,上网找了不少资料,都没有找到一种比较适合的方案,就自己

权重随机算法的java实现

一.概述 平时,经常会遇到权重随机算法,从不同权重的N个元素中随机选择一个,并使得总体选择结果是按照权重分布的.如广告投放.负载均衡等. 如有4个元素A.B.C.D,权重分别为1.2.3.4,随机结果中A:B:C:D的比例要为1:2:3:4. 总体思路:累加每个元素的权重A(1)-B(3)-C(6)-D(10),则4个元素的的权重管辖区间分别为[0,1).[1,3).[3,6).[6,10).然后随机出一个[0,10)之间的随机数.落在哪个区间,则该区间之后的元素即为按权重命中的元素. 实现方法

带权随机数问题--根据权重随机选择一条路径

最近工作中遇到了一个根据权重随机选择一条路径的问题,一时没有啥好方案,参考借鉴了网上的经验,得出了如下解决方案: 思路:1.求权重的和,对(0,权重之歌和]区间进行划分,每个权重占用长度为权重的区间: 2.产生一个在(0,权重之和]区间的等概率随机数: 3.该随机数落在哪个区间,则该区间对应的权重的映射为本次产生的带权随机数. 1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.List; 4

加权重随机算法

场景:有N个合作方,每一个合作方都有一定的权重,按权重随机选择一个合作方 typedef struct { string k;//partner_id string v;//value string m;//0:number 1:ratio }Bookpartner_count_listInfo; string GetRandNumRatio( vector<Bookpartner_count_listInfo> arpartner_count_list) { int weight = 0;