当前位置:网站首页>Leetcode daily question - 710 Random numbers in the blacklist
Leetcode daily question - 710 Random numbers in the blacklist
2022-06-28 20:40:00 【Did HYK write the algorithm today】
List of articles
subject
Given an integer n And a No repetition Blacklist integer array blacklist . Design an algorithm , from [0, n - 1] Select one of any integers in the range Not joined The blacklist blacklist The integer of . Any within the above scope and not on the blacklist blacklist All integers in should have Equal possibility Returned .
Optimize your algorithm , Minimize the call language built-in The number of random functions .
Realization Solution class :
- Solution(int n, int[] blacklist) Initialize integer n And being blacklisted blacklist The integer of
- int pick() Returns a range of [0, n - 1] And not on the blacklist blacklist Random integers in
Example
Example 1:
Input [“Solution”, “pick”, “pick”, “pick”, “pick”, “pick”, “pick”,
“pick”] [[7, [2, 3, 5]], [], [], [], [], [], [], []]
Output [null, 0, 4,1, 6, 1, 0, 4]
explain Solution solution = new Solution(7, [2, 3, 5]); solution.pick(); // return 0, whatever [0,1,4,6] All integers are OK . Be careful , For each of these pick Call to ,// 0、1、4 and 6 The return probability of must be equal ( That is, the probability is 1/4).
solution.pick(); // return 4 solution.pick(); // return 1
solution.pick(); // return 6 solution.pick(); // return 1 solution.pick(); // return 0 solution.pick(); // return 4
Tips :
1 <= n <= 109
0 <= blacklist.length <= min(105, n - 1)
0 <= blacklist[i] < n
blacklist All values in are Different
pick At most called 2 * 104 Time
Ideas
The idea of commenting on the district boss
Ideas :
- Blacklist length is s, We from [0, N-s) Take a random value from , This random value may be in the blacklist , What do I do ?
- [0, N-s) Internal elements , If there is i In the blacklist , So in [N-s, N) in , There must be i Elements are not in the blacklist
- Yes [0, N-s) Blacklist elements and [N-s, N) Mapping elements that are not in the blacklist m, It must be one-to-one , It doesn't matter how they correspond
- from [0, N-s) Take a random value from r, If r Not on the blacklist , Go straight back to ; If r In the blacklist , be m[r] It must not be on the blacklist , return m[r]
Answer key
from random import randint
class Solution:
def __init__(self, N, blacklist):
""" :type N: int :type blacklist: List[int] """
self.s = N - len(blacklist)
# Less than s A collection of blacklist elements
b_lt_s = {i for i in blacklist if i < self.s}
# Greater than s A collection of non blacklist elements
# Equivalent to :w_gt_s = {i for i in range(self.s, N)} - set(blacklist), Thank you for your comments
w_gt_s = {
*range(self.s, N)} - set(blacklist)
# Mapping , Use zip Make it convenient
# Equivalent to :self.m = {k: v for k,v in zip(b_lt_s, w_gt_s)}
self.m = dict(zip(b_lt_s, w_gt_s))
def pick(self):
""" :rtype: int """
r = randint(0, self.s-1)
return r if r not in self.m else self.m[r]
边栏推荐
- The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
- 员工薪资管理系统
- How to analyze the relationship between enterprise digital transformation and data asset management?
- [graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!
- Bitbucket 使用 SSH 拉取仓库失败的问题
- ANR无响应介绍
- Apisik helps Middle East social software realize localized deployment
- Relevant calculation of sphere, etc
- Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
- 字符和整数
猜你喜欢

2022 welder (elementary) special operation certificate examination question bank and answers

with torch. no_ Grad(): reason for using

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?

Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication

UESTC (shenhengtao team) & JD AI (Mei Tao team) proposed a structured dual stream attention network for video Q & A, with performance SOTA! Better than the method based on dual video representation

2022 P cylinder filling test exercises and online simulation test

28 rounds of interviews with 10 companies in two and a half years

不同框架的绘制神经网络结构可视化

RT-Thread线程同步与线程通信

Visualization of neural network structure in different frames
随机推荐
员工薪资管理系统
LeetCode每日一题——30. 串联所有单词的子串
字符和整数
嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
[try to hack] cobalt strike (I)
Input and output real data
APISIX 助力中东社交软件,实现本地化部署
输入和输出实型数据
MySQL system error occurred 1067
Why does next() in iterator need to be forcibly converted?
数据标准化处理
3. integrate listener
[learning notes] factor analysis
Troubleshooting of pyinstaller failed to pack pikepdf
Flatten of cnn-lstm
Bitbucket 使用 SSH 拉取仓库失败的问题
How to open an account in great wisdom? Is it safe
大智慧上怎么进行开户啊, 安全吗
酷学院华少:如何在SaaS赛道里做成一家头部公司
LeetCode每日一题——515. 在每个树行中找最大值