当前位置:网站首页>【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
边栏推荐
- Système de distribution Unity Composants en tissu (y compris les dépendances d'appel dynamique)
- curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection
- Common configuration of jupyterlab
- [hybrid programming JNI] details of JNA in Chapter 11
- 简述unity的模型动画功能
- C language: a simple calculator is implemented by using code many times
- WordPress collection plug-ins are recommended to be free collection plug-ins
- CVPR 2022 | 美团技术团队精选论文解读
- Unity布料系统_Cloth组件(包含动态调用相关)
- Partage de trois méthodes de sommation automatique dans un tableau Excel
猜你喜欢

数据清洗工具flashtext,效率直接提升了几十倍数

大龄程序员的一些出路

【LeetCode】1984. Minimum difference between highest and lowest of K scores

开放世界机甲游戏-Phantom Galaxies

L'outil de nettoyage des données flashtext améliore directement l'efficacité de plusieurs dizaines de fois
![leetcode:1567. 乘积为正数的最长子数组长度【dp[i]表示以i结尾的最大长度】](/img/a4/c5c31de7a0a3b34a188bfec0b5d184.png)
leetcode:1567. 乘积为正数的最长子数组长度【dp[i]表示以i结尾的最大长度】
![Selenium电脑上怎么下载-Selenium下载和安装图文教程[超详细]](/img/ec/1c324dcf38d07742a139aac2bab02e.png)
Selenium电脑上怎么下载-Selenium下载和安装图文教程[超详细]

Open world mecha games phantom Galaxy
![leetcode:152. Product maximum subarray [consider DP of two dimensions]](/img/c8/af6a4c969affd151a5214723dffb57.png)
leetcode:152. Product maximum subarray [consider DP of two dimensions]

树莓派初步使用
随机推荐
【混合编程jni 】第九篇之Jni总结
【图像处理基础】基于matlab GUI图像直方图均衡化系统【含Matlab源码 1924期】
Leetcode - the best time to buy or sell stocks
WordPress collection plug-ins are recommended to be free collection plug-ins
Système de distribution Unity Composants en tissu (y compris les dépendances d'appel dynamique)
MacOS環境下使用HomeBrew安裝[email protected]
Design of master-slave replication system
Unity3D插件 AnyPortrait 2D骨骼动画制作
Reading graph augmentations to learn graph representations (lg2ar)
DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
Introduction to operator
分享三種在Excel錶格中自動求和的方法
LabVIEW Arduino TCP/IP远程智能家居系统(项目篇—5)
The sharp sword of API management -- eolink
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
Unity 设置Material、Shader的方法
Installation avec homebrew dans un environnement Mac OS [email protected]
npm 命令提示Error: EACCES: permission denied
Which platform is the safest for buying stocks and opening accounts? Ask for sharing
[hybrid programming JNI] details of JNA in Chapter 11