当前位置:网站首页>【710. 黑名单中的随机数】
【710. 黑名单中的随机数】
2022-06-26 22:46:00 【Sugar_wolf】
来源:力扣(LeetCode)
描述:
给定一个整数 n 和一个 无重复 黑名单整数数组 blacklist 。设计一种算法,从 [0, n - 1] 范围内的任意整数中选取一个 未加入 黑名单 blacklist 的整数。任何在上述范围内且不在黑名单 blacklist 中的整数都应该有 同等的可能性 被返回。
优化你的算法,使它最小化调用语言 内置 随机函数的次数。
实现 Solution 类:
Solution(int n, int[] blacklist)初始化整数n和被加入黑名单blacklist的整数int pick()返回一个范围为[0, n - 1]且不在黑名单blacklist中的随机整数
示例 1:
输入
["Solution", "pick", "pick", "pick", "pick", "pick", "pick", "pick"]
[[7, [2, 3, 5]], [], [], [], [], [], [], []]
输出
[null, 0, 4, 1, 6, 1, 0, 4]
解释
Solution solution = new Solution(7, [2, 3, 5]);
solution.pick(); // 返回0,任何[0,1,4,6]的整数都可以。注意,对于每一个pick的调用,
// 0、1、4和6的返回概率必须相等(即概率为1/4)。
solution.pick(); // 返回 4
solution.pick(); // 返回 1
solution.pick(); // 返回 6
solution.pick(); // 返回 1
solution.pick(); // 返回 0
solution.pick(); // 返回 4
提示:
- 1 <= n <= 109
- 0 <= blacklist.length <= min(105, n - 1)
- 0 <= blacklist[i] < n
- blacklist 中所有值都 不同
- pick 最多被调用 2 * 104 次
方法 :黑名单映射
设 blacklist 的长度为 m。
考察一个特殊的例子:所有黑名单数全部在区间 [n − m, n) 范围内。此时我们可以直接在 [0, n − m) 范围内取随机整数。
这给我们一个启示,对于在 [0, n − m) 范围内的黑名单数,我们可以将其映射到 [n − m, n) 范围内的非黑名单数(白名单数)上。每次 pick() 时,仍然可以在 [0, n − m) 范围内取随机整数(设其为 x),那么:
如果 x 不在黑名单中,则直接返回 x;
如果 x 在黑名单中,则返回 x 映射到 [n − m, n) 范围内的白名单数。
我们可以在初始化时,构建一个从 [0, n − m) 范围内的黑名单数到 [n − m, n) 的白名单数的映射:
将 [n − m, n) 范围内的黑名单数存入一个哈希集合 black;
初始化白名单数 w = n − m;
对于每个 [0, n − m) 范围内的黑名单数 b,首先不断增加 w 直至其不在黑名单中,然后将 b 映射到 w 上,并将 w 增加一。
代码:
class Solution {
private:
unordered_map<int, int> mp;
int bound;
public:
Solution(int n, vector<int> &blacklist) {
int m = blacklist.size();
bound = n - m;
unordered_set<int> black;
for (int b: blacklist) {
if (b >= bound) {
black.emplace(b);
}
}
int w = bound;
for (int b: blacklist) {
if (b < bound) {
while (black.count(w)) {
++w;
}
mp[b] = w++;
}
}
}
int pick() {
int x = rand() % bound;
return mp.count(x) ? mp[x] : x;
}
};
执行用时:108 ms, 在所有 C++ 提交中击败了89.64%的用户
内存消耗:68.6 MB, 在所有 C++ 提交中击败了65.49%的用户
复杂度分析
时间复杂度:初始化为 O(m), pick() 为 O(1),其中 m 是数组 blacklist 的长度。在初始化结束时, [n − m, n) 内的每个数字要么是黑名单数,要么被一个黑名单数所映射,因此 white 会恰好增加 m 次,因此初始化的时间复杂度为 O(m)。
空间复杂度: O(m)。哈希表需要 O(m) 的空间。
author:LeetCode-Solution
边栏推荐
- LabVIEW Arduino tcp/ip remote smart home system (project part-5)
- [machine learning] - Introduction to vernacular and explanation of terms
- 在线协作文档综合评测 :Notion、FlowUs、Wolai、飞书、语雀、微软 Office、谷歌文档、金山文档、腾讯文档、石墨文档、Dropbox Paper、坚果云文档、百度网盘在线文档
- 【图像处理基础】基于matlab GUI图像直方图均衡化系统【含Matlab源码 1924期】
- Different subsequence problems I
- Using C to operate SQLSERVER database through SQL statement tutorial
- Three solutions for improving embedded software development environment
- Common configuration of jupyterlab
- NPM command prompt error: eacces: permission denied
- 电子协会 C语言 1级 29 、 对齐输出
猜你喜欢

从位图到布隆过滤器,C#实现

Product design in the extreme Internet Era

MATLAB and MySQL database connection and data exchange (based on ODBC)
![leetcode:710. Random numbers in the blacklist [mapping thinking]](/img/ec/a3faeae6636bc3d0d9536962fdd9af.png)
leetcode:710. Random numbers in the blacklist [mapping thinking]

树莓派初步使用

數據清洗工具flashtext,效率直接提昇了幾十倍數

在线协作文档综合评测 :Notion、FlowUs、Wolai、飞书、语雀、微软 Office、谷歌文档、金山文档、腾讯文档、石墨文档、Dropbox Paper、坚果云文档、百度网盘在线文档

Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
![[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!](/img/78/ece6da9a26c9d93988dac30ce93f99.png)
[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!

CVPR 2022 | 美团技术团队精选论文解读
随机推荐
树莓派初步使用
Comprehensive evaluation of online collaboration documents: note, flowus, WOLAI, Feishu, YuQue, Microsoft office, Google Docs, Jinshan docs, Tencent docs, graphite docs, Dropbox paper, nutcloud docs,
如何写好测试用例以及go单元测试工具testify简单介绍
这个算BUG吗?乱填的字母是否可以关闭
Different subsequence problems I
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
[mathematical modeling] spanning tree based on Matlab GUI random nodes [including Matlab source code 1919]
Weaving dream collection plug-ins are recommended to be free collection plug-ins
Which platform is the safest for buying stocks and opening accounts? Ask for sharing
VB. Net class library (advanced version - 1)
leetcode:152. 乘积最大子数组【考虑两个维度的dp】
Which securities company is the most convenient, safe and reliable for opening an account
Are there any risks for the top ten securities companies to register and open accounts? Is it safe?
Brief analysis of the self inspection contents of the blue team in the attack and defense drill
JupyterLab 常用配置
[fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]
[mixed programming JNI] Part 12 jnaerator
【混合编程jni 】第九篇之Jni总结
Is it safe for CICC fortune to open an account? I want to open an account to speculate in stocks.
LabVIEW Arduino TCP/IP远程智能家居系统(项目篇—5)