LeetCode 710. Random Pick with Blacklist - 随机抽取不在黑名单里的数字

该问题要求设计一个算法,从[0, n - 1]中随机选择不在黑名单blacklist内的数字,确保每个可用数字的概率相等。通过建立映射,将黑名单中[0, n-m-1]的数字与[n-m, n-1]的可选数字对应,可以实现一次随机选取并减少对内置随机函数的调用次数。" 80222463,2973597,Eureka服务注册与安全验证配置详解,"['微服务', 'Spring Cloud', 'Eureka Server', '服务治理', '安全']
摘要由CSDN通过智能技术生成

You are given an integer n and an array of unique integers blacklist. Design an algorithm to pick a random integer in the range [0, n - 1] that is not in blacklist. Any integer that is in the mentioned range and not in blacklist should be equally likely to be returned.

Optimize your algorithm such that it minimizes the number of calls to the built-in random function of your language.

Implement the Solution class:

  • Solution(int n, int[] blacklist) Initializes the object with the integer n and the blacklisted integers blacklist.
  • int pick() Returns a random integer in the range [0, n - 1] and not in blacklist.

Example 1:

Input
["Solution", "pick", "pick&
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值