当前位置:网站首页>710. random numbers in the blacklist
710. random numbers in the blacklist
2022-06-26 19:52:00 【North_ dust】
Looked at the leetcode My daily question , Topic link :710. Random numbers in the blacklist
The title is as follows :
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
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
The first thought to see this problem is Put the data not in the blacklist into a new data , Take another number at random , But the submission found space , Memory limit exceeded , The code is as follows :
class Solution {
private int n;
private int[] blacklist;
private Set<Integer> set;
private int[] arr;
private int len;
private Random random = new Random();
public Solution(int n, int[] blacklist) {
this.n = n;
this.blacklist = blacklist;
this.set = new HashSet();
this.len = blacklist.length;
this.arr = new int[n-len];
for(int i =0;i < blacklist.length;i++){
set.add(blacklist[i]);
}
int index = 0;
for(int i = 0;index < arr.length;i++){
if(!set.contains(i)){
arr[index] = i;
index++;
}
}
}
public int pick() {
int i = random.nextInt(arr.length);
return arr[i];
}
}
Because the blacklist number is not repeated and is in [0,n) In this range , Here you can try use map Let's record [n-len,n) Interval data and will **[0,n-len)** Within the interval Blacklist Data mapped to **[n-len,n)** in be not in The blacklist On the number of In this way . The code is as follows :
class Solution {
private int n;
private int[] blacklist;
private int len;
private Random random;
private Map<Integer,Integer> map;
public Solution(int n, int[] blacklist) {
this.n = n;
this.blacklist = blacklist;
this.map = new HashMap();
random = new Random();
this.len = blacklist.length;
for(int i =0;i < blacklist.length;i++){
if(blacklist[i]>=n-len){
map.put(blacklist[i],blacklist[i]);
}
}
int end = n-1;
int index = 0;
for(int i = 0;i < blacklist.length;i++){
int value = blacklist[i];
if(value <n-len){
while(map.containsKey(end)){
end--;
}
map.put(value,end);
end--;
}
}
}
public int pick() {
int i = random.nextInt(n-len);
if(map.containsKey(i)){
return map.get(i);
}
return i;
}
}
边栏推荐
猜你喜欢

Kubernetes resource topology aware scheduling optimization
![[kubernetes] kubernetes principle analysis and practical application (under update)](/img/37/40b8317a4d8b6f9c3acf032cd4350b.jpg)
[kubernetes] kubernetes principle analysis and practical application (under update)

Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印

Six necessary threat tracking tools for threat hunters

Unity——Mathf. Similarities and differences between atan and atan2

Refresh the strong pointer assignment problem in the HP-UX system of Sanguan

西瓜书重温(七): 贝叶斯分类器(手推+代码demo)

mongoDB的三种基础备份方法

Create a time blocker yourself

MySQL recharge
随机推荐
[kubernetes] kubernetes principle analysis and practical application (under update)
Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles
SSO微服务工程中用户行为日志的记录
Three basic backup methods of mongodb
数据库SQL语句撰写
stm32和电机开发(直流有刷电机和步进电机)
2022/02/14 line generation
超分之VRT
What are the specific steps for opening a stock account? Is it safe to open an account online?
Case description: the competition score management system needs to count the competition scores obtained by previous champions and record them in the file. The system has the following requirements: -
Tiktok practice ~ sharing module ~ generate short video QR code
Deep learning: numpy
微服务版单点登陆系统(SSO)
抖音实战~搜索页面~视频详情
Tiktok practice ~ search page ~ video details
回溯思路详解
WebView load pdf
50 lines of code to crawl TOP500 books and import TXT documents
Preliminary analysis of serial port printing and stack for arm bare board debugging
关于不等式取值转义的思路