[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. 1 <= N <= 1000000000
  2. 0 <= B.length < min(100000, N)
  3. [0, N) does NOT include N. See interval notation.

Example 1:

Input:
["Solution","pick","pick","pick"]
[[1,[]],[],[],[]]
Output: [null,0,0,0]

Example 2:

Input:
["Solution","pick","pick","pick"]
[[2,[]],[],[],[]]
Output: [null,1,1,1]

Example 3:

Input:
["Solution","pick","pick","pick"]
[[3,[1]],[],[],[]]
Output: [null,0,0,2]

Example 4:

Input:
["Solution","pick","pick","pick"]
[[4,[2]],[],[],[]]
Output: [null,1,3,1]

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution‘s constructor has two arguments, N and the blacklist Bpick has no arguments. Arguments are always wrapped with a list, even if there aren‘t any.

s

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

时间: 2024-08-17 05:20:21

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

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

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

带权重的随机算法及实现

在游戏开发过程中,经常会遇到生成一波带权重的随机怪物或是掉落List中物品带权重的情况,总结下我的算法以及实现方法. 直接上代码 using System.Collections.Generic; using System; public class RandomHelper { /// <summary> /// 算法: /// 1.每个元素权重+1命名为w,防止为0情况. /// 2.计算出总权重n. /// 3.每个元素权重w加上从0到(n-1)的一个随机数(即总权重以内的随机数),得到

带黑洞的随机游走问题

对于无黑洞的随机游走问题可以使用线性方程组求解,对于有黑洞的随机游走问题就无法使用线性方程组进行求解了. 有黑洞的随机游走问题举例: 随机给定一个魔方状态,随机旋转期望通过多少步才能旋转到目标状态? 醉汉回家问题:一个人在x点处喝醉了,在N维空间中游走,它回到出发点的概率是多少?求p(N) zhihu 一个整型数字x=6000,每次增长101的概率为49.32%,每次减少100元的概率为50.68%,问最终x&tt;7000的概率是多少? 显然,这个问题相当于一个随机游走问题,一共有100~70

java Math.random()生成从n到m的随机整数

Java中Math类的random()方法可以生成[0,1)之间的随机浮点数.而double类型数据强制转换成int类型,整数部分赋值给int类型变量,小数点之后的小数部分将会丢失. 如果要生成[0,n]的随机整数的话,只需要Math.random()乘以n+1,生成[0,n+1)的浮点数,再强制类型转换为int类型,只取其整数部分,即可得到[0,n]的整数. int b=(int)(Math.random()*10);//生成[0,9]之间的随机整数. int temp=m+(int)(Mat

[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

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

leetcode_398 Random Pick Index(Reservoir Sampling)

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 spa