当前位置:网站首页>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);
}
}
边栏推荐
- SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time
- Simple interactive operation of electron learning (III)
- vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
- Tourism Management System
- Today's sleep quality record 71 points
- A few minutes before work, I found out V-model and The difference between sync
- Tcpdump command usage details
- 【Kotlin 第三方 】coil koltin协程图片加载库Coil类似Glide的图片加载第三方
- Introduction and use of plantuml
- Cisco exam -- redundant network
猜你喜欢

转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程

数字货币:影响深远的创新

正则系列之量词(Quantifiers)

Groups and ranges of regular series

Introduction and use of plantuml

window10安装wsl(一)(WslRegisterDistribution ERROR)

map容器

Appium自动化测试基础 — 补充:Desired Capabilities参数介绍

今日睡眠质量记录71分

I graduated from college in 14 years and changed to software testing in 3 months. My monthly salary was 13.5k. At the age of 32, I finally found the right direction
随机推荐
Where can the courses purchased by CSDN be accessed
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
What class loading mechanisms does the JVM have?
今日睡眠质量记录71分
Data enhancement of semi supervised learning
Multiple smart pointers
A few minutes before work, I found out V-model and The difference between sync
思科--WAN 的概念考试外部工具
locust的使用
软件测试之「 性能测试」总结,新手上路必会知识点
阿洛迷茫后的思考
Use and function of spark analyze command map join broadcast join
Efficiency improvement - encourage personalized container development environment
Today's sleep quality record 71 points
工作中非常重要的测试策略,你大概没注意过吧
Appium自动化测试基础 — 补充:Desired Capabilities参数介绍
Pytorch nn.functional.unfold()的简单理解与用法
91. (cesium chapter) cesium rocket launch simulation
Favorite transaction code management tool in SAP GUI
Daily question brushing record (10)