当前位置:网站首页>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);
}
}
边栏推荐
猜你喜欢

使用 EMQX Cloud 实现物联网设备一机一密验证

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

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

"Trust machine" empowers development

今日睡眠质量记录71分

转--拿来即用:分享一个检查内存泄漏的小工具

Cisco -- highly available and reliable network examination

MySQL -- index of InnoDB storage engine

Introduction and use of plantuml

【Kotlin 第三方 】coil koltin协程图片加载库Coil类似Glide的图片加载第三方
随机推荐
Explain the volatile keyword
shell 流程控制
Arlo's thinking after confusion
Metauniverse may become a new direction of Internet development
104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
nn. Parameter] pytoch feature fusion adaptive weight setting (learnable weight use)
聊一聊Zabbix都监控哪些参数
[untitled]
Chen Tianqi's machine learning compilation course (free)
Ffmpeg learning notes
tcpdump命令使用详解
Single step debugging analysis of rxjs observable of operator
[C language] detailed explanation of malloc function [easy to understand]
vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
Unable to climb hill sort, directly insert sort
Cloud Vulnerability Global Database
Pytorch nn.functional.unfold()的简单理解与用法
Tourism Management System
Using securecrtportable to remotely connect virtual machines
数字货币:影响深远的创新