当前位置:网站首页>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);
}
}
边栏推荐
- [untitled]
- rxjs Observable of 操作符的单步调试分析
- Use and function of spark analyze command map join broadcast join
- Pytorch nn.functional.unfold()的简单理解与用法
- What class loading mechanisms does the JVM have?
- Preparation of functional test report
- leetcode - 287. Find duplicates
- Hide the creation and use of users
- Digital currency: far-reaching innovation
- 力扣 710. 黑名单中的随机数
猜你喜欢

Cutefishos system~

Ffmpeg learning notes

“信任机器”为发展赋能

447 Bili Bili noodles warp 1

MySQL -- index of MyISAM storage engine

使用3DMax制作一个象棋棋子

Today's sleep quality record 71 points

今日睡眠质量记录71分

The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 release | geek headlines

Introduction and use of plantuml
随机推荐
447 Bili Bili noodles warp 1
若干互联网暴露面的收敛及处置建议
数字化转型道阻且长,如何迈好关键的第一步
Turn -- bring it and use it: share a gadget for checking memory leaks
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
Cisco -- an external tool for WAN's concept examination
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
Demo program implementation of QT version Huarui camera
[daily training] 66 add one-tenth
Groups and ranges of regular series
el-input文本域字数限制,超过显示变红并禁止输入
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条
El input text field word limit, beyond which the display turns red and input is prohibited
Mixconv code
转载csdn文章操作
Mysql5.7 set password policy (etc. three-level password transformation)
思科--高可用和高可靠网络考试
Flynk SQL client uses comparison and is familiar with official documents
Cisco test -- the concept and configuration test of routing
Turn -- the underlying debugging principle of GDB is so simple