当前位置:网站首页>710. 黑名单中的随机数
710. 黑名单中的随机数
2022-06-26 19:48:00 【北_尘】
看了下leetcode的每日一题,题目链接:710. 黑名单中的随机数
题目描述如下:
给定一个整数 n 和一个 无重复 黑名单整数数组 blacklist 。设计一种算法,从 [0, n - 1] 范围内的任意整数中选取一个 未加入 黑名单 blacklist 的整数。任何在上述范围内且不在黑名单 blacklist 中的整数都应该有 同等的可能性 被返回。
优化你的算法,使它最小化调用语言 内置 随机函数的次数。
实现 Solution 类:
Solution(int n, int[] blacklist) 初始化整数 n 和被加入黑名单 blacklist 的整数 int
pick() 返回一个范围为 [0, n - 1] 且不在黑名单 blacklist 中的随机整数
提示:
1 <= n <= 109
0 <= blacklist.length <= min(105, n - 1)
0 <=blacklist[i] < n
blacklist 中所有值都 不同
pick 最多被调用 2 * 104 次
看到这道题的第一思路是 将不在黑名单的数据放到一个新的数据中,再随机取一个数,但提交却发现空间,超过内存限制,代码如下:
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];
}
}
由于黑名单数不重复且在[0,n)这个区间内,这里可以试试 用map来记录一下 [n-len,n) 区间的数据并将 **[0,n-len)**区间内在 黑名单中 的数据映射到 **[n-len,n)**中 不在 黑名单 的数上 这个方式解决。代码如下:
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;
}
}
边栏推荐
- Reading notes: process consulting III
- 限流设计及实现
- MySQL - table creation and management
- MySQL stored procedure
- Uni app uses canvas to draw QR code
- Project practice 4: user login and token access verification (reids+jwt)
- Can I open an account online? Is it safe?
- Six necessary threat tracking tools for threat hunters
- 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: -
- Is it safe to open an account for CICC Wealth Online?
猜你喜欢

Tiktok practice ~ sharing module ~ short video download (save to photo album)

Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用

Filebeat安装及使用
![[recommended collection] these 8 common missing value filling skills must be mastered](/img/ab/353f74ad73ca592a3f97ea478922d9.png)
[recommended collection] these 8 common missing value filling skills must be mastered

Convex hull problem

Tree array

vue中缓存组件keep-alive

Super VRT

好物推荐:移动端开发安全工具

SSO微服务工程中用户行为日志的记录
随机推荐
Solidity - contract inheritance sub contract contains constructor errors and one contract calls the view function of another contract to charge gas fees
关于不等式取值转义的思路
Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles
To: Apple CEO Cook: great ideas come from constantly rejecting the status quo
Wechat applet uniapp left slide delete with Delete Icon
SSO微服务工程中用户行为日志的记录
飞天+CIPU体为元宇宙带来更大想象空间
2022/02/14 line generation
抖音实战~搜索页面~扫描二维码
vue中缓存组件keep-alive
Tiktok practice ~ homepage video ~ pull-down refresh
Tiktok practice ~ sharing module ~ copy short video link
转:实事求是
Convex hull problem
Basic and necessary common plug-ins of vscade
Nftgamefi chain game system development detailed solution - chain game system development principle analysis
开发者调查:Rust/PostgreSQL 最受喜爱,PHP 薪水偏低
Wechat applet custom pop-up components
Analysis on development technology of NFT meta universe chain game system
Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用