当前位置:网站首页>Force buckle 710 Random numbers in the blacklist
Force buckle 710 Random numbers in the blacklist
2022-07-01 23:02:00 【Three watch ghost】
Title source :https://leetcode.cn/problems/random-pick-with-blacklist/
General meaning :
Given a range of numbers n, An array of blacklists , Design a class return 0 To n - 1 Between random numbers that are not in the blacklist , It is required that the probability of each number being returned should be equal
Ideas
Set the blacklist array length to m, Then the number of returned numbers is n - m individual
So you can set the random number bound by n - m
- If the number in the blacklist is greater than or equal to bound, Then you don't have to do anything
- If the number in the blacklist is less than bound, You can use a hash table to map it to [bound, n - 1] in . In this way, the blacklist generated by the random number generator is less than bound The number of hours , Directly map to another number
Code :
public class Pick {
// The mapping blacklist is less than bound Number of numbers
Map<Integer, Integer> map;
int bound;
Random random;
public Pick(int n, int[] blacklist) {
map = new HashMap<>();
int m = blacklist.length;
// initialization bound
bound = n - m;
random = new Random();
// Save the hash table of blacklist numbers
Set<Integer> blackSet = new HashSet<>();
for (int num : blacklist) {
if (num >= bound) {
blackSet.add(num);
}
}
// Mapping index
int idx = bound;
// Traverse the blacklist
for (int num : blacklist) {
// If the blacklist number is less than bound, Map it to [bound, n - 1] The number that is not in the blacklist and has not been mapped
if (num < bound) {
// If the current number to be mapped is in the blacklist , Indexes +1
while (blackSet.contains(idx)) {
idx++;
}
map.put(num, idx++);
}
}
}
public int pick() {
// Generate random number
int x = random.nextInt(bound);
// Return random number , If the random number is map in , Returns the number of its mapping
return map.getOrDefault(x, x);
}
}
边栏推荐
- 元宇宙可能成为互联网发展的新方向
- Multi picture alert ~ comparison of Huawei ECs and Alibaba cloud ECS
- Explain the use of locksupport in detail
- 有些能力,是工作中学不来的,看看这篇超过90%同行
- Hide the creation and use of users
- Kubernetes create service access pod
- Unable to climb hill sort, directly insert sort
- SAP intelligent robot process automation (IRPA) solution sharing
- 死锁的处理策略—预防死锁、避免死锁、检测和解除死锁
- [JUC learning road day 9] barrier derivatives
猜你喜欢
转载csdn文章操作
Daily question brushing record (10)
vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
Multi picture alert ~ comparison of Huawei ECs and Alibaba cloud ECS
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
Stimulate new kinetic energy and promote digital economy in multiple places
Cisco -- highly available and reliable network examination
工作中非常重要的测试策略,你大概没注意过吧
思科--WAN 的概念考试外部工具
Turn -- the underlying debugging principle of GDB is so simple
随机推荐
Today's sleep quality record 71 points
Multi picture alert ~ comparison of Huawei ECs and Alibaba cloud ECS
Mysql5.7 set password policy (etc. three-level password transformation)
Stimulate new kinetic energy and promote digital economy in multiple places
Armbain系统根分区空间不足处理
What class loading mechanisms does the JVM have?
ECMAScript 2022 was officially released. Have you heard about it?
正则系列之量词(Quantifiers)
Simple interactive operation of electron learning (III)
Design of ESP automatic download circuit
Friendly serial assistant tutorial_ How to configure friendly serial port debugging assistant - tutorial on using friendly serial port debugging assistant
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
[JUC learning road day 8] condition
思科--WAN 的概念考试外部工具
rxjs Observable of 操作符的单步调试分析
[image segmentation] 2021 segformer neurips
Flink SQL command line connection yarn
tcpdump命令使用详解
map容器
SAP intelligent robot process automation (IRPA) solution sharing